Explanation of Cast and Implicit Type Conversion in JavaScript

  • 2021-11-13 06:18:55
  • OfStack

Directory 1. Implicit conversions conversions in double equals
Boolean type conversion
"+" and "-"
2. Censor type conversion
new String and ''
Summarize

1. Implicit conversion

What is the execution result of the following statement?


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

Answer:

A. undefined = = null is true; undefined = = = null is false

B. isNaN returns true when passing in NaN or a value that can be converted to NaN, and "100" is converted to Number first > 100, not NaN, so false is returned

C. parseInt ("1a") resolves only the part preceded by a number, i.e. only "1", and parseInt ("1a1") = = = 1 is also true

D. [] is an empty array and returns true

Conversion in double equal sign

After warming up, let's look at 1 code:


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

As a result, true is output.

In fact, [] is false, because in the equal sign comparison, if one side is Boolean, the data on the left and right sides will be preferentially converted to Number.
Namely:


Number([]); // 0
Number(false); // 0

So [] is false.

However, when an if decision is made, the data is converted to Boolean type,

Namely:


Boolean([]); // true

So you can output true.

Summarize some skills in judging double equal signs:

If there is NaN before and after the double equal sign, the 1 law returns false;; If there are Boolean values before and after the double equal sign (0, 1 is also counted), the law of 1 is converted into numbers and then compared, (false is 0, true is 1); If there are strings before and after the double equal sign, there are three situations: If the other end is an object, the object is converted using toString or ValueOf; The other end is a number, and the string is converted into a number; The other end is a string, which is directly compared; The other 1 law returns false. If one end is a number and the other end is a string such as' 3 ', refer to Article 3. The other end is an object, and the results of toString or ValueOf of the object are compared, and the other 1 law returns false; null and undefined do not type conversion, but they are equal;

Boolean type conversion


var test = new Boolean();
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());

The answer is:

false false false false false

The principle is as follows:

When called as a constructor (with the operator new), Boolean () will convert its parameters to a Boolean value and return an Boolean object containing that value. Note that the Boolean object returned here can be obtained by toString or valueOf; If called as a function (without the operator new), Boolean () will only convert its arguments to an original Boolean value and return this value-cast; If the value parameter is omitted or set to 0,-0, null, "", false, undefined, or NaN, the object is set to false. Otherwise, it is set to true (even if the value parameter 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);

What is the output result?

Results: 122 32 NaN2 NaN

Analysis:

The addition of numbers and strings will turn numbers into strings, so the result is string splicing, and the first sentence outputs "122"; + "2", where the 1 yuan operator +, converts the string into a number, so 1 + + "2" = 1 +2 = 3, and then merges with the following strings, so the second sentence outputs "32"; The minus sign converts both sides of the minus sign into numbers first, while Number ("A") and Number ("B") result in NaN. In subtraction operation, as long as one side is NaN, the result is NaN, so the third sentence is the splicing of NaN and "2", and the result is NaN2 (mentioned in point 4.1, one side of the double equal sign is NaN and the result is false); According to the analysis of the first sentence, the result of "A"-"B" is NaN, which is added with the number 2, and the result is NaN

2. Censor type conversion

For code var a = 10.42; Take out the integer part of a, which of the following codes is correct?


A. parseInt( a );
B. Math.floor( a );
C. Math.ceil( a );
D. a.split('.')[0];

Answer: AB

Many people will choose ABC after looking at one eye

Analysis:

A. parseInt is converted to an integer, defaults to decimal, and the result is 10;

B. floor is rounded down and the result is 10-floor means floor, rounded down to assist memory;

C. ceil is rounded up and the result is 11;

D. split operand must be regular or string, and the result is TypeError.

new String and ''

Which of the following execution results is true?


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

Answer:

A: Detailed below;

B: Detailed below;
C: Number ([]) The result is 0, so the C option is correct;
D: Objects are always true, but why is it false to judge an empty object = = true or = = = true?

According to our summary of the double equals sign, when one side of the double equals sign is Boolean, both sides will be converted to Number, so the console tests empty objects = = true or = = = true, which is actually executing empty objects = = 1 or = = = 1, so it will return false. Therefore, if you want to verify that objects are always true, you should use! {} and! ! {}! {}; //false; ! ! {}; //true

E: Since Boolean (0) = = false! 0=true, correct.

To talk about the AB option, the new keyword is used to call the function constructor, and the result becomes unexpected.

First of all, we should know that when using new to call a constructor, if a reference object (array, object, function, etc.) is returned internally, the anonymous object created by new will be overwritten.

If a primitive type is returned (in the absence of an explicit return, an undefined is actually returned), then the anonymous object created by new is returned.

In this way,

In the A option, a string is returned in the constructor, so the final constructor returns an anonymous object created by new, that is, an empty object.

In the B option, the constructor returns a string object internally, so the final constructor returns this object.

Summarize


Related articles: