JavaScript implicit type conversion

Source: Internet
Author: User

in programming, you have to convert a data type to another data type. Where a function or method call and a cast are called display transformations, conversely, the "intentional" conversion, in the case of "unknowingly", is called a hermit conversion.

Hermit type conversions in operators

Compare common hermit type conversions, which are present in many languages. This is just about JavaScript.

1, "+" operator

function Add () {     var a = ' 2 ';     var b =2;     var C = a + B;     Console.info (c);         Output     Console.info (typeof c);  Output string} add ();

here the JS (ECMASCRIPT) engine will connect the B programming string ' 2 ' to A and finally become "22". The "+" operators in many languages are handled this way. The process is implicit, and the JS engine helps us to do it. And with the "+" feature, it is convenient to convert the number type to a string type. For example:

var number = 12;var c = ' + Number;console.info (typeof number);   Numberconsole.info (typeof c);        String

2, "-" operator

"-" can be a unary operator (negative) or a two-dollar operator (subtraction), and "-" will also perform implicit type conversions, such as

var var1 = 234;var var2 = "432"; var C = Var1-var2;console.info (c);              -198console.info (typeof c);       Number

It can be seen that the "-" feature is the opposite of "+", which converts the string B hermit into a number and then the subtraction operation. With this feature, it is convenient to convert a string to number

var = ' var3 '; var var4 = Var3-"; Console.info (VAR4)             ; 12console.info (typeof VAR4);   Number

3, * and \ Operators

and the "+", "-" multiplication and division are also implicitly typed conversions (converted to number and then evaluated).

var = "1", var var2 = "2", var var3 = "Var1", var var4 = var1 * Var2;console.info (VAR4);          2console.info (typeof var4)    //numbervar var5 = var4/var3; Console.info (VAR5);          0.1console.info (typeof Var5);   Number

4, Increment decrement operator (front, rear)

Such operators apply to values of any data type, and for different types of values, the operator follows the following translation rules:

1, if it is a string containing a valid numeric character, first convert it to a numeric value (same number ()), and then perform the addition and subtraction operation, the string becomes a numeric variable

2. If a valid numeric string is not included, the value of the variable is set to Nan and the string variable becomes a numeric variable.

3. If the Boolean value is False, convert to 0 to perform the operation.

4. If the Boolean value is True, convert to 1 to perform the operation.

5, if it is a floating point value, directly add minus 1 operation.

6, if it is an object, first call the valueof () method of the object pair, then convert according to the first 5 points, if the result is Nan, call the ToString () method and then apply the preceding rule to judge.

5. Logical operators (!, &&, | |)

Logical NON (!) The operator first converts his operation value to a Boolean value through the Boolean () function, and then the negation.

Logic and (&&)

1, if the first operand is converted to true after a Boolean (), the second action value is returned, otherwise the first value (not the Boolean () converted value) is returned

2, if there is an action value of NULL. return null

3, if there is an action value of Nan, return Nan

4, if there is an action value of undefined, return undefined

Logical OR (| |), if an action value is not a Boolean value, follow these rules:

1. Returns the second action value if the first action value is converted to False after a Boolean (), otherwise returns the first action value (not a Boolean () converted value)

2, for undefined, null and Nan processing rules and logic with the same

6. Relational operator (<,>,<=,>=)

As with the above operator, the operation value of the relational operator can also be any type, so it is necessary for the system to perform implicit type conversion when using non-numeric types to participate in the comparison.

1, two operation values are numeric, direct comparison

2, two characters are strings, string encoding is compared

3, one value, the other is converted to a numeric value for comparison

4, an operand is an object, call valueof () |tostring (), followed by 1, 2 to compare

5, an Action value is a Boolean value, then it is converted to a numeric value, then the comparison.

Tips:nan is a very special value, not equal to any type of value, including itself, and returns False when it compares size to any type of value.

7, equal operator = = and = = =

The equality operator also compares an implicit conversion of an action value

1, Boolean, string and numeric comparison, converted to a numeric value after the comparison

2, null and undefined are equal

3, and Nan sentence, etc., return false

TIPS: = = and = = = Difference

1, for Array,object and other advanced types, there is no difference: "Address" comparison

2, for the basic type = = judgment value is equal, = = = Not only judge the value, and determine whether the type is equal.

3. The basic type differs from the advanced type: = = Converts the advanced type to the underlying type, makes a "value" comparison, and = = = Returns false directly

Implicit conversions in statements

There are also implicit type conversions in JavaScript syntax, where if,while,for in,for in defines object literals from identifiers to string the implicit conversion

var ary = [1,3,5,7];for (var A in ary) {    alert (A + ":" + typeof a);}

the index of an array is actually a string type.

Implicit conversions when alert is present

String.prototype.fn = function () {return this};var a = ' hello '; alert (typeof A.fn ()); -->objectalert (A.fn ()); -->hello

The FN method is added to the string prototype, which returns this, which is the current instance object, and it is understandable that typeof A.fn () is an object type and can alert (A.fn ()) instead implicitly convert the object to the string "Hello" Show.

Similarly, the numeric type is the same:

Number.prototype.fn = function () {return This};var a = 10;alert (typeof A.fn ());//-->objectalert (A.fn ()); -->10

Summarize

I believe that the implicit type conversion in JS is more kinky than that, because JS is a weak type of language, resulting in coding flexibility, you can compare different types of operations (JS implicit conversion), which may make people feel surprised and uncomfortable. Whether this mechanism is prone to error is not too much to worry about, because most of the cases we don't need to convert, we seldom make different types of comparisons (if so, not more convenient).


JavaScript implicit type conversion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.