About the = Operator and if Statement of js
As we all know, these two are related to boolean values. The operator = returns a Boolean value, and the if () or ternary operator? : Boolean values are determined. In js, The = Operator and = operator are different. We all know that the previous one will convert the type, but this one will not follow, and if () type conversion is also performed.
Let's talk about =
First, let's look at several examples:
Console. log ("123" = 123); // true
Console. log ([1, 3] = 123); // false
Console. log ([1, 2] = "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
Let's talk about the principle. When the data types on both sides are different, data types will be converted. null, undefined, and NaN are three special things. Remember them. Then let's explain them in the previous figure.
These are five basic data types plus an object. Let's see how they are compared. All those who have studied mathematics know that there are 15 situations in which we will analyze them.
1. number -- boolean
Except that values 1 and 0 can be converted to true and false respectively, the other values are neither true nor false. This is my own experiment.
2. number -- object
First, call the object. toString () method, and then call the Number () method to convert the string to number. Except the array, it is basically impossible to be equal.
3. number -- undefined
Directly not equal
4. number -- string
String calls the Number () method and compares it with the number
5. number -- null
Directly not equal
6. boolean -- object
Here we need to separate the array and non-array. If the non-array is neither true nor false, the array will first call toString () and then call the Boolean () method, then compare it with boolean.
7. boolean -- undefined
Undefined is neither true nor false.
8. boolean -- string
String calls the Number and then compares it to the first case.
9. boolean -- null
Null is neither true nor false.
10. object -- undefined
Directly not equal
11. object -- string
Call the toString () method of the object and then compare it.
12. object -- null
Directly not equal to, except null is equal to null
13. undefined -- string
Directly not equal
14. undefined -- null
Equal
15. string -- null
Directly not equal
As a matter of fact, we can use a picture to extract the essence:
After the = sign is finished, let's take a look at if. In fact, there are many such cases as if, for example, three elements, such as |, &, and so on, which can be converted to boolean values, what are the rules at this time?
If it is directly set to false, all values except false are true.
Null, undefined, 0, "", except for the rest, NaN is basically true.
Now let's take a look at the above questions:
Console. log ("123" = 123); // true directly calls Number ()
Console. log ([123, 3] =); // false [, 3]. after toString () is equal to "1, 2, 3", call Number ("1, 2, 3") and then change to NaN.
Console. log ([, 3] = ", 3"); // true
Console. log (null = undefined); // true
Console. log (NaN = NaN); // remember false.
Console. log ([] = []); // The type of false is the same, and the pointer position is compared.
Console. log ([] =! []); // True // first! [] Is false, because in if, [] is true, then compare [] = false, [] First Call toString to "", then call Number ("") the value 0 to 0 can be changed to false.
Console. log ({}= = "[object Object]") // you do not need to explain true. Call toString () to convert it to [object Object].
Well, you still need to analyze more to remember the problem.