A brief analysis of implicit type conversion in JavaScript

  • 2020-03-30 00:43:57
  • OfStack

If you explicitly cast one type to another through a function or method call, it is called a display cast, and conversely, it is called an implicit cast. Google and wikipedia don't have the words "show cast" or "implicit cast." Let's call it that.

I. implicit type conversion in operation

1, the + operator

var a = 11, b = '22'; 
var c = a + b; 

Here the engine is going to turn a into a string "11" and then it's going to connect to b, and it's going to be "1122". Some people wonder, why don't you just turn b into the number 22 and do the arithmetic addition, so that c is 33. There's no reason why, but the js engine specifies string concatenation rather than arithmetic addition when one of the two sides of the operator "+" is a numeric type and the other is a string type. The operator "+" makes it easy to convert Number to String. Such as

var a = 11; 
alert(typeof a); //-->number 
a = a + ''; 
alert(typeof a); //-->string 

2. "-" operator

The "-" can be either unary (negative) or binary (subtraction). Such as

var a = 11, b = '5'; 
var c = a - b; 
alert(typeof c); //--> number 

In contrast to the "+" above, the string b is implicitly converted to the number 5 and arithmetic subtracted. Using this feature, you can easily convert a String to a Number

var a = '11'; 
a = a - ''; 
alert(typeof a);// -->number 

Implicit type conversion in statements

1, if

var obj = {name:'jack'} 
    //do more 

This is going to convert obj implicitly to a Boolean type

2, while

var obj = {name:'jack'} 
    //do more 

With the if

3. Type conversion when for in
Implicit conversion from identifier to string occurs when defining an object literal.

var person = {'name':'jack',"age":20,school:'PKU'}; 
for(var a in person){ 
    alert(a + ": " + typeof a); 

In this case, single/double quotation marks are added to the name and age respectively to emphasize that they are of type String, and single/double quotation marks are not added to the school. Let's iterate over the properties of the object to see its type. The school is also implicitly converted to String.

The index of an array is also a string type. It's amazing, but it's true. Such as

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

Implicit type conversion in alert

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

We added an fn method to the String prototype, which returns this. We know that this can be interpreted as an instance object of the current class. Since it is an object, typeof a.fn() naturally returns an object.
The key is the last alert(a.fn()), which returns an object that is implicitly converted to the string "hello".

The same thing happens with number types, such as

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 is implicitly converted to a number when alert(a.fn()) is raised.

Related articles: