Follow me on javascript's implicit cast

  • 2020-11-03 22:01:04
  • OfStack

JavaScript data types are divided into 6 kinds, respectively null, undefined, boolean, string, number, object.

object is a reference type, and the other five are primitive or primitive types. We can use the typeof method to print out which type a particular belongs to. Comparing variables of different types requires conversion, which is also called implicit conversion. Implicit t transformations usually occur when operators are added, subtracted, multiplied and divided, equal to, less than, greater than, etc.


typeof '11' //string 
typeof(11)  //number
'11' < 4 //false

1. Conversion of basic types

Now let's start with addition, subtraction, multiplication and division:

1. Add a number to a string, and the number will turn into a string.

2. Subtract a string and turn the string into a number. If the string is not pure, it is converted to NaN. The string minus the number is also 1. Subtract the two strings and convert them to Numbers first.

3. Multiplying, dividing, greater than, less than and subtracting is the same thing.


// Implicit conversion  + - * == / 
// + 
10 + '20' //'2010'
// -
10 - '20' //-10
10 - 'one' //NaN
10 - '100a' //NaN
// *
10*'20' //200
'10'*'20' //200
// /
20/'10' //2
'20'/'10' //2
'20'/'one'  //NaN

4. The order of addition operations is sensitive

Mixed expressions like this can be confusing because JavaScript is sensitive to the order of operations. For example, the expression: 1+2+"3"; / / "33"

Since the addition operation is combined from the left (i.e. the left associative law), it is equivalent to the following expression :(1+2)+"3"; / / "33"

Instead, the expression is: 1+"2"+3; //"123" evaluates to the string "123". The left associative law wraps the addition operation to the left of the expression in parentheses.

5. Let's look at group 1 ==

1). undefined equals null

2). When comparing a string with a number, turn the string into a number

3). When the number is a Boolean comparison, the number is reversed

4). When a string is compared with a Boolean, it is converted to a number


// ==
undefined == null; //true
'0' == 0;    //true, String rotation 
0 == false; //true, Boolean switch Numbers 
'0' == false;    //true, Conversion of two Numbers 
null == false;   //false
undefined == false;  //false

Seven false values: false, 0, -0, "", NaN, null and undefined, all other values are truth

6. NaN, not a number

NaN is a special value that indicates that the result of some arithmetic operations, such as taking the square root of a negative number, is not a number. The methods parseInt() and parseFloat() return this value if they cannot parse the specified string. For a function that normally returns a significant number, you can also use Number.NaN to illustrate its error condition.


Math.sqrt(-2)
Math.log(-1)
0/0
parseFloat('foo')

For many beginners of JavaScript, its first pitfall is that calling typeof usually returns something you wouldn't expect:


console.log(typeof NaN); // 'Number'

In this case, NaN does not mean a number, its type is a number. Do you understand the & # 63;
Because typeof returns a string, there are six types: "number", "string", "boolean", "object", "function", "undefined"

Stay calm, because there's a lot of confusion down there. Let's compare two NaN:


var x = Math.sqrt(-2);
var y = Math.log(-1);
console.log(x == y); // false

Maybe that's because we're not using strictly equivalent (===) operations? Apparently not.


var x = Math.sqrt(-2);
var y = Math.log(-1);
console.log(x === y); // false

How about directly comparing the two NaN?


console.log(NaN === NaN); // false

Since there are so many ways to represent a non-number, it doesn't make sense for a non-number to be equal to another non-number for NaN.

But of course, the solution is there now. Let's look at the 1 global function isNaN:


console.log(isNaN(NaN)); // true

Alas, isNaN() has many drawbacks of its own:


console.log(isNaN('hello')); // true
console.log(isNaN(['x'])); // true
console.log(isNaN({})); // true

This leads to many different solutions. One of them takes advantage of the non-reflective nature of NaN (for example, look at the notes of Kit Cambridge)


// Implicit conversion  + - * == / 
// + 
10 + '20' //'2010'
// -
10 - '20' //-10
10 - 'one' //NaN
10 - '100a' //NaN
// *
10*'20' //200
'10'*'20' //200
// /
20/'10' //2
'20'/'10' //2
'20'/'one'  //NaN

0

Fortunately, however, in the upcoming ECMAScript 6, one of the ES113en.isNaN () methods provides reliable NaN value detection.
In other words, true is returned only if the argument is the real NaN


// Implicit conversion  + - * == / 
// + 
10 + '20' //'2010'
// -
10 - '20' //-10
10 - 'one' //NaN
10 - '100a' //NaN
// *
10*'20' //200
'10'*'20' //200
// /
20/'10' //2
'20'/'10' //2
'20'/'one'  //NaN

1

2. Conversion of reference types

The comparison between the basic types is relatively simple. The comparison between reference types and base types is more complicated. First, the reference types are converted to base types, and then compared as described above.

1. Reference type converters are true.

For example, an empty array is a reference type as long as it is an object, so [] is true. Reference types are converted to Numbers or strings using valueOf() or toString(); The object itself inherits valuOf() and toString(), and you can also customize valueOf() and toString(). Depending on the object, the inherited valueOf() is converted to a string, number, or itself, whereas the object with toString is converted to a string. valueOf() is called by default for general objects.

1). Call valueOf() when the object is converted to Numbers;

2). When the object is turned into a string, call toString();

Take a look at this example:


// Implicit conversion  + - * == / 
// + 
10 + '20' //'2010'
// -
10 - '20' //-10
10 - 'one' //NaN
10 - '100a' //NaN
// *
10*'20' //200
'10'*'20' //200
// /
20/'10' //2
'20'/'10' //2
'20'/'one'  //NaN

2

valueOf() is called when the object is turned into a number, and toString() is called before that; So I guess the valueOf method looks like this. So example 0 == [] would make more sense. But anyway, [] ends up going to 0.


var valueOf = function (){
 var str = this.toString(); // First call toString(), Turn into a string 
 //...
}
0 == []; // true, 0 == [].valueOf(); -> 0 == '0' -> 0 == 0;

Custom valueOf() and toString();

The custom valueOf() and toString() both exist and call valueOf() by default. If there is only toString(), call toString();


// Implicit conversion  + - * == / 
// + 
10 + '20' //'2010'
// -
10 - '20' //-10
10 - 'one' //NaN
10 - '100a' //NaN
// *
10*'20' //200
'10'*'20' //200
// /
20/'10' //2
'20'/'10' //2
'20'/'one'  //NaN

4

Removing valueOf() calls toString().


// Implicit conversion  + - * == / 
// + 
10 + '20' //'2010'
// -
10 - '20' //-10
10 - 'one' //NaN
10 - '100a' //NaN
// *
10*'20' //200
'10'*'20' //200
// /
20/'10' //2
'20'/'10' //2
'20'/'one'  //NaN

5

What happens if I return something else?


// Implicit conversion  + - * == / 
// + 
10 + '20' //'2010'
// -
10 - '20' //-10
10 - 'one' //NaN
10 - '100a' //NaN
// *
10*'20' //200
'10'*'20' //200
// /
20/'10' //2
'20'/'10' //2
'20'/'one'  //NaN

6

Other objects call valueOf() to a different type:


// Implicit conversion  + - * == / 
// + 
10 + '20' //'2010'
// -
10 - '20' //-10
10 - 'one' //NaN
10 - '100a' //NaN
// *
10*'20' //200
'10'*'20' //200
// /
20/'10' //2
'20'/'10' //2
'20'/'one'  //NaN

7

A comparison between reference types is a comparison of memory addresses and does not require implicit conversion.

[] == [] //false address is different

var a = [];
b = a;
b == a //true

2. Explicit conversion

Explicit transformations are simpler and can be done directly using classes as methods.

Number([]); //0
String([]); / /"
Boolean([]); //true

There are simpler conversion methods.

3 + "// string '3'
Plus '3' // the number 3
!!'3' // true

That's the end of this article. I have introduced the implicit cast of javascript in detail. I hope it is helpful for you to learn.


Related articles: