Implicit conversion

What is the result of the following statement?

A. undefined == null
B. isNaN("100")
C. parseInt("1a") === 1
D. [ ] instanceof Array
Copy the code

The answer:

A. Undefined == null; Undefined === null is false

C. parseInt(“1a”) parseInt(“1 “) parseInt(“1 “) parseInt(“1 “); ParseInt (“1a1 “) === 1 is also true D. [] is an empty array and returns true

The transformation in the double equal sign

Here’s another piece of code after you’ve warmed up:

if ([]) {
  console.log(true);
}
Copy the code

The result prints true. [] = ‘Number’; [] = ‘Number’;

Number([]); / / 0
Number(false); / / 0
Copy the code

So [] is false. But, however, an if judgment converts the data to a Boolean type, that is:

Boolean([]); // true
Copy the code

So we can print true.

Summarize some skills of double equal sign judgment:

  1. Return false if NaN is followed by a double equal sign;
  2. If there is a Boolean value before and after the double equal sign (0,1 count), convert it to a number and compare it (false = 0, true = 1);
  3. If there are strings before and after the double equal sign, there are three cases:
    • On the other end are objects, which are converted using either toString or ValueOf.
    • On the other end are numbers, string to number;
    • On the other end are strings, direct comparisons;
    • Return false for all others.
  4. If one end is a number and the other is a string such as ‘3’ refer to item 3. The toString or ValueOf of the object is used for comparison. Otherwise false is returned.
  5. Null and undefined do not convert, but they are equal;

Boolean type conversion

var test = new Boolean(a);console.log(test);

var test = new Boolean(0);
console.log(test.valueOf());

var test = new Boolean(null);
console.log(test.valueOf());

var test = new Boolean("");
console.log(test.valueOf());

var test = new Boolean(NaN);
console.log(test.valueOf());
Copy the code

The answer is:

false false false false false

The principle is as follows:

  1. When called as a constructor (with operator new), Boolean() converts its argument to a Boolean value and returns a Boolean object containing the value. Note that this returns a Boolean object, either toString or valueOf;
  2. When called as a function (without the operator new), Boolean() simply converts its argument to a raw Boolean value and returns that value — a cast;
  3. If the value argument is omitted, or if it is 0, -0, null, “”, false, undefined, or NaN, the object is set to false. Otherwise set to true (even if the value argument is the string “false “).

“+” and “-“

console.log(1 + "2" + "2");
console.log(1 + +"2" + "2");
console.log("A" - "B" + "2");
console.log("A" - "B" + 2);
Copy the code

What is the output?

The result is 122 32 NaN2 NaN

  1. Adding a number to a string converts the number to a string, so the result is string concatenation, and the first sentence is “122”;
  2. The unary operator + converts the string to a number, so 1+ +”2 “= 1+2 = 3, which is merged with the following string, so the second sentence prints” 32″;
  3. The minus sign converts both sides of the minus sign to numbers first, and Number(“A “) and Number(“B “) result in NaN. In the subtraction operation, if either side is NaN, the result is NaN. Therefore, the third sentence is the concatenation of NaN and “2”, and the result is NaN2 (as mentioned in point 4.1, The double equals sign, where one of the parties is NaN, results in false);
  4. The result of “A “-“B” is NaN, and when added to the number 2, the result is NaN

Second, cast

For code var a = 10.42; Taking the integer part of A, which of the following is correct?

A. parseInt( a ); B. Math.floor( a ); C. Math.ceil( a ); D. a.s plit ('. ') [0];Copy the code

A. parseInt is converted to an integer in base 10 by default, resulting in 10. A. floor b. floor C. floor D. floor C. Ceil is rounded upward and the result is 11. D. split operands must be regular or strings, resulting in TypeError.

New String and ‘ ‘

Which of the following results is true?

A. 'foo' == new function(){ return String('foo'); }; B. 'foo' == new function(){ return new String('foo'); }; C. [] == 0 D. ! [] E: ! 0Copy the code

The answer:

A: I’ll tell you more.

B: More on that. C: Number([]) = 0 D: Objects are always true, but why is it that an empty object == true or === true gets false? According to our summary of double equals, when one side of double equals is a Boolean, both sides are converted to Number, so the console tests the empty object == true or === true, but it’s actually executing the empty object ==1 or === 1, so it returns false, so, If you want to verify that an object is always true, you should use it! {} and!!!!! {}! {}; // false; !!!!! {}; E: Boolean(0) == false, therefore! 0=true.

As for the AB option, the result is unexpected because the new keyword is used and the function constructor is called. The first thing we need to know is,

  1. When a constructor is called using new, the anonymous object created by new is overridden if a reference object (array, object, function, etc.) is returned internally.
  2. If a primitive type is returned (undefined without an explicit return), the anonymous object created by new is returned.

If A returns A string inside the constructor, the constructor returns an anonymous object created by new, which is an empty object. In option B, the constructor returns a string object inside, so the final constructor returns that object.