Implicit type conversion in JavaScript
This article provides a detailed analysis of implicit type conversion in JavaScript. If you need some help, please refer to it.
If you call a function or method, explicitly convert a type to another type, which is called display conversion. On the contrary, it is called implicit type conversion. Google and Wikipedia do not find the word "display type conversion" and "implicit type conversion. This is what we call it now.
1. Implicit conversions in operations
1. "+" Operator
The Code is as follows:
Var a = 11, B = '22 ';
Var c = a + B;
Here, the engine will first convert a into a string "11" and then connect B, to "1122 ". Some people may wonder why we don't convert B into number 22 and then perform arithmetic addition. In this case, c is 33. No. When either the operator "+" is a number or a string, the js engine requires string Join Operations instead of arithmetic addition operations. Using the "+" operator, you can easily convert a Number to a String. For example
The Code is as follows:
Var a = 11;
Alert (typeof a); // --> number
A = a + '';
Alert (typeof a); // --> string
2, "-" Operator
"-" Can be a unary operator (negative) or a binary operator (subtraction. For example
The Code is as follows:
Var a = 11, B = '5 ';
Var c = a-B;
Alert (typeof c); // --> number
In contrast to the above "+", string B is implicitly converted to number 5 before arithmetic subtraction. Using this feature, you can easily convert String to Number.
Copy the Code as follows:
Var a = '11 ';
A = -'';
Alert (typeof a); // --> number
Ii. implicit type conversion in statements
1, if
The Code is as follows:
Var obj = {name: 'jack '}
If (obj ){
// Do more
}
Here, obj is implicitly converted to the Boolean type.
2, while
The Code is as follows:
Var obj = {name: 'jack '}
While (obj ){
// Do more
}
Same as if
3. type conversion for in
An implicit conversion from an identifier to a string occurs when the object literal volume is defined.
The Code is as follows:
Var person = {'name': 'jack', "age": 20, school: 'pku '};
For (var a in person ){
Alert (a + ":" + typeof );
}
Here, "name" and "age" are added with single/double quotation marks respectively to emphasize the String type. "school" does not contain single/double quotation marks. Let's review the attributes of this object and view its type. School is implicitly converted to the String type.
The index of the array is actually a string type. This is amazing, but it does. For example
The Code is as follows:
Var ary = [1, 3, 5, 7];
For (var a in ary ){
Alert (a + ":" + typeof );
}
Iii. implicit type conversion in alert
The Code is as follows:
String. prototype. fn = function () {return this };
Var a = 'hello ';
Alert (typeof a. fn (); // --> object
Alert (a. fn (); // --> hello
Added a fn method to the String prototype. this method returns this. We know this can be understood as an instance object of the current class. Since this is an object, typeof. fn () naturally returns an object.
The key is the final alert (a. fn (). a. fn () returns an object but is implicitly converted to the string "hello" for display.
The same situation occurs in the numeric type, as shown in figure
The Code is as follows:
Number. prototype. fn = function () {return this };
Var a = 10;
Alert (typeof a. fn (); // --> object
Alert (a. fn (); // --> 10
A. fn () returns the object type, but it is implicitly converted to a number when alert (a. fn.