If you explicitly convert a certain type to another type through a function or method call, it is called explicit conversion, and the opposite is called implicit type conversion. The words "display type conversion" and "implicit type conversion" are not found in Google and Wikipedia. Let's call it that.
1. Implicit type conversions in operations
1. “+” operator
Copy the code code as follows:
var a = 11, b = '22';
var c = a + b;
Here the engine will first turn a into the string "11" and then connect it with b, which becomes "1122". Some people may wonder, why not turn b into the number 22 and then perform arithmetic addition, in which case c will be 33. There is no reason. When one of the two sides of the operator "+" is a numeric type and the other is a string type, the js engine stipulates to perform string concatenation instead of arithmetic addition. Using the feature of operator "+", Number can be easily converted into String. like
Copy the code code as follows:
var a = 11;
alert(typeof a); //-->number
a = a + '';
alert(typeof a); //-->string
2. "-" operator
"-" can be a unary operator (negative operation) or a binary operator (subtraction operation). like
Copy the code code as follows:
var a = 11, b = '5';
var c = a - b;
alert(typeof c); //--> number
Contrary to the "+" above, the string b will be implicitly converted into the number 5 and then arithmetic subtraction will be performed. Using this feature, you can easily convert String into Number
Copy the code code as follows:
var a = '11';
a = a - '';
alert(typeof a);// -->number
2. Implicit type conversions in statements
1, if
Copy the code code as follows:
var obj = {name:'jack'}
if(obj){
//do more
}
Here obj will be implicitly converted to Boolean type
2. while
Copy the code code as follows:
var obj = {name:'jack'}
while(obj){
//do more
}
Same as if
3. Type conversion during for in
An implicit conversion from identifier to string occurs when an object literal is defined.
Copy the code code as follows:
var person = {'name':'jack',"age":20,school:'PKU'};
for(var a in person){
alert(a + ": " + typeof a);
}
Here, single/double quotation marks are added to name and age respectively to emphasize that they are of String type, and single/double quotation marks are not added to school. We traverse the properties of the object to see its type. It is found that school is also implicitly converted to String type.
The index of the array is actually a string type. It's amazing, but it's true. like
Copy the code code as follows:
var ary = [1,3,5,7];
for(var a in ary){
alert(a + ": " + typeof a);
}
3. Implicit type conversion when alerting
Copy the code code as follows:
String.prototype.fn = function(){return this};
var a = 'hello';
alert(typeof a.fn()); //-->object
alert(a.fn()); //-->hello
A fn method is added to the String prototype, which returns this. We know that this can be understood as an instance object of the current class. Since it is an object, typeof a.fn() naturally returns object.
The key is the last alert(a.fn()). What a.fn() returns is obviously an object, but it is implicitly converted into a string "hello" for display.
The same thing happens with numeric types, like
Copy the code code as follows:
Number.prototype.fn = function(){return this};
var a = 10;
alert(typeof a.fn());//-->object
alert(a.fn()); //-->10
a.fn() returns an object type, but it is implicitly converted into a number when alert(a.fn()) is used.