As we all know, these two are related to Boolean values, operator = = Returns a Boolean value, and if () or ternary operator?: will go to judge the Boolean value, in the JS = = operator and = = = operator is not the same as we all know, the previous one will be type conversion, and this will not, and if () There is also a type conversion.
First, say = =
Let's start with a few examples:
Console.log ("123" ==123);//true
Console.log ([1,2,3]==123);//false
Console.log ([1,2,3]== ");//true
Console.log (null==undefined);//true
Console.log (Nan==nan);//false
Console.log ([]==[]);//false
Console.log ([]==![]); /true
Console.log ({}== "[Object Object]")//true
First of all, the principle, when the data type is different on both sides, will be type conversion, Null,undefined,nan is three special East, to remember them, and then we have a picture to explain it
Here are five basic data types plus an object, so let's see how they compare to each other. Learn mathematics know, a total of 15 cases, below we one kind to analyze
1.number--boolean
In addition to 1 and 0 can be converted to true and false, the rest is neither equal to true, nor equal to false, is my own experiment, hey.
2.number--object
First call the Object.ToString () method, and then call the number () method to convert the string to number, except for the array, which is basically impossible to equal.
3.number--undefined
Directly not equal to
4.number--string
String calls the number () method, and then compares it with number
5.number--null
Directly not equal to
6.boolean--object
To divide the array and non-arrays, the non-arrays are neither equal to true nor equal to False, the array calls ToString () before calling the Boolean () method, and then the Boolean comparison.
7.boolean--undefined
undefined is neither equal to true nor equal to False
8.boolean--string
String calls number and then compares it back to the first case
9.boolean--null
Null is neither equal to true nor equal to False
10.object--undefined
Directly not equal to
11.object--string
Call the ToString () method of object, and then compare
12.object--null
Directly not equal to, except null equals NULL
13.undefined--string
Directly not equal to
14.undefined--null
Equals
15.string--null
Directly not equal to
Actually said so much, can use a picture to refine the essence:
After saying the = = number, we look at if, in fact, if this situation has a lot of, such as three, such as | |,&&, will exist when the conversion to a Boolean value, this time the rules?
In case of a direct false, the remainder of false is true.
null,undefined,0, "", NaN except that the rest is basically true.
Let's take a look at the above question:
Console.log ("123" ==123);//true call number directly ()
Console.log ([1,2,3]==123);//false [1,2,3].tostring () is equal to "all-in-all", then calls number ("the") and then becomes Nan, of course not.
Console.log ([1,2,3]==];//true this explains
Console.log (null==undefined);//true
Console.log (Nan==nan);//false this, remember.
Console.log ([]==[]),//false same type, compare pointer position
Console.log ([]==![]); /true//First! [] is false because in if, [] is true, then the comparison []==false,[] first calls ToString to "", and then the call number ("") becomes 0,0 can become false.
Console.log ({}== "[Object Object]")//true this doesn't have to be explained, call ToString () into [Object Object]
Well, still need more analysis to remember the problem ah, hey.
Talk about JS's = = operator and if statement