An article gets JavaScript type conversion of interview common

  • 2021-07-13 04:16:47
  • OfStack

Why do you want to say this thing? Give me one interview question to tell me its motivation.

The title is as follows:


var bool = new Boolean(false);
if (bool) {
 alert('true');
} else {
 alert('false');
}

The result is true! ! !

In fact, what type conversion, operator priority, these things are the most basic. There are detailed descriptions in the Rhinoceros book. But I seldom go through the first five chapters of Rhinoceros. . .

For example, in the priority section, many books teach us, "Don't recite the priority order, just put parentheses if you are uncertain." We do this when we write code.

But what is the reality? This kind of question will come out during the interview, so let you do it. . . I really don't know the meaning of this kind of question. . .

So much for complaining. This article tries to solve the type conversion problem and try to memorize the table on page 49 of JS Authoritative Guide.

What are the false values?

A total of 6:


0 Or +0 , -0 , NaN
""
false
undefined
null

The above order is arranged according to the basic type.

Other than that, the 1 law is not! ! Even in the following form:


Infinity
'0' , 'false' , " " (Space characters) 
 Any reference type :[],{},function(){}

if (a & & b) is correctly understood as: a & & b Evaluates the expression and then converts to the Boolean type.

& & It is a short-circuit syntax. After evaluation, it is not a Boolean type, and it is not a Boolean value conversion on both sides.

For example, 2 & & The result of 3 is 3, not true.

So if (a & & b), as we usually understand it, "if a and b are true at the same time", is a wrong description.

Other basic types are converted into strings, basic and expected 1 samples:


console.log("" + null);   // "null"
console.log("" + undefined); // "undefined"
console.log("" + false);   // "false"
console.log("" + true);   // "true"
console.log("" + 0);     // "0"
console.log("" + NaN);    // "NaN"
console.log("" + Infinity); // "Infinity"

Converting other basic types into numbers requires special memory:


console.log(+null);     // 0
console.log(+undefined);   // NaN
console.log(+false);     // 0
console.log(+true);     // 1
console.log(+"");      // 0
console.log(+'1');      // 1
console.log(+'1x');     // NaN 

Where null, the null character is 0, and undefined is NaN.

Above, the basic type conversion is clear.

Let's take a look at converting reference types to basic types.

Reference type is converted to Boolean, always true

Reference type to string

1. Give priority to calling the toString method (if any) to see if the return result is of the original type. If so, convert it to a string and return it.

2. Otherwise, call the valueOf method (if any) to see if the return result is of the original type, and if so, convert it to a string and return.

3. Other errors.

Convert reference types to numbers

1. Call the valueOf method first (if any) to see if the return result is of basic type. If so, convert it to a number and return it.

2. Otherwise, call the toString method (if any) to see if the return result is a basic type, and if so, convert it to a number and return.

3. Other errors.

First, let's look at what the common reference types toString and valueOf return.


var a = {};
console.dir(a.toString());  // "[object Object]"
console.dir(a.valueOf());  //  Object itself 
var b = [1, 2, 3];
console.dir(b.toString());  // "1,2,3"
console.dir(b.valueOf());  //  Object itself 
var c = [[1],[2]];
console.dir(c.toString());  // "1,2"
console.dir(c.valueOf());  //  Object itself 
var d = function() {return 2};
console.dir(d.toString());  // "function() {return 2}"
console.dir(d.valueOf());  //  Object itself 

Therefore, the corresponding conversion to strings and numbers is:


var a = {};
console.dir(a + "");     // "[object Object]"
console.dir(+a);       // NaN
var b = [1, 2, 3];
console.dir(b + "");     // "1,2,3"
console.dir(+b);       // NaN
var c = [[1],[2]];
console.dir(c + "");     // "1,2"
console.dir(+c);       // NaN
var d = function() {return 2};
console.dir(d + "");     // "function () {return 2}"
console.dir(+d);       // NaN

Let's report an error again:


var a = {};
a.toString = function() {return {};}
console.log("" + a);     //  Report an error 
console.log(+a)       //  Report an error 

The above type conversion rules are basically finished.

Finally, let's say 1 evil "= ="

The interview questions are as follows:


var a = false;
var b = undefined;
if (a == b) {
 alert('true');
} else {
 alert('false');
}

I thought true would pop up. Oh, my God! Why false?

Ha ha. . .

Double equal sign, if the types on both sides are different, implicit conversion will occur. The 75 pages of the Rhinoceros Book are summarized as follows:

1. null and undefined are equal.

2. Numbers and strings are converted into numbers and then compared.

3. If there is true or false, convert it to 1 or 0, and then compare it.

4. If there is a reference type, call valueOf first.

5. The rest are not equal.

Therefore, there are:


console.log(undefined == false); // false
console.log(null == false);   // false
console.log(0 == false);     // true
console.log(NaN == false);    // false
console.log("" == false);    // true

0 = = false is true according to Article 3.

"" = = false is true according to Article 3 and becomes "" = = 0, and then according to Article 2.

Another example of Article 4:


0 Or +0 , -0 , NaN
""
false
undefined
null
0

The result above is true for the following reasons:

The valueOf of [[2]] is the object itself, not the primitive type.

The result of trying to call toString is' 2 '.

So it becomes a comparison between '2' and the number 2. According to Article 2, equality. WTF! !

Finally, using "= = =" will eliminate these problems.


Related articles: