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
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
VaR A = 11; alert (typeof A); // --> numbera = a + ''; alert (typeof A); // --> string
2, "-" Operator
"-" Can be a unary operator (negative) or a binary operator (subtraction. For example
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.
VaR A = '11'; A = A-''; alert (typeof A); // --> Number
Ii. implicit type conversion in statements
1, if
VaR OBJ = {Name: 'jack'} If (OBJ) {// do more}
Here, obj is implicitly converted to the boolean type.
2, while
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.
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
VaR ary = [1, 3, 5, 7]; for (var a in ary) {alert (a + ":" + typeof );}
Iii. implicit type conversion in alert
String. prototype. fn = function () {return this}; var A = 'hello'; alert (typeof. FN (); // --> objectalert (. 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
Number. prototype. fn = function () {return this}; var A = 10; alert (typeof. FN (); // --> objectalert (. FN (); // --> 10
A. FN () returns the object type, but it is implicitly converted to a number when alert (A. FN.