Type (x) |
Type (y) |
Result |
Type (x) = type (y) |
X = y |
Otherwise... |
False |
Null |
Undefined |
True |
Undefined |
Null |
True |
Number |
String |
X = toNumber (y) |
String |
Number |
ToNumber (x) = y |
Boolean |
(Any) |
ToNumber (x) = y |
(Any) |
Boolean |
X = toNumber (y) |
String or Number |
Object |
X = toPrimitive (y) |
Object |
String or Number |
ToPrimitive (x) = y |
Example 1:
[0] = true;
First, the toNumber of ture will be implemented, and the result is 1. The formula is as follows and the conversion is [0] = 1.
Then [0] will be toString (), the result is "0", and the formula is converted to "0" = 1
Then "0" will be toNumber (), the result is 0, and the formula is converted to 0 = 1
Finally, according to type (x) = type (y), the formula is converted to 0 = 1
Therefore, if [0] = true, false is returned.
Example 2:
"Potato" = true;
First, "true" is converted to "potato" = 1, and the result is 1.
Then "potato" will be toNumber and the result will be NaN. The formula is converted to NaN = 1.
According to typeof (x) = typeof (y), the formula is converted to NaN = 1
So "potato" = true will get false
Example 3:
Object with getValue
Str = new Number (1 );
Str. toString = function () {return "2 "};
Str = 1;
Here, typeof str = "object", so toPrimitive is applied to str. Here, valueOf is used to obtain 1.
So the formula is converted to 1 = 1
So str = 1 to get true
Example 4:
Object with toString
Var obj = {
ToString: function () {return "2 "}
};
Obj = 1
Here typeof obj = "object", toPrimitive for obj, first valueOf to get the object, continue toString () to get "2", formula for "2" = 1
Perform the toNumber operation on "2" to get 2, and convert the formula to 2 = 1.
So obj = 1 to get false.
Example 5:
[] = []
The left and right are different objects, so false
! [] = []
First! [] = False, here [] is an existing object. If you force it to be of the bool type, pass !! [] To get true, so! [] = False. The formula is set to false = []
Then false is set to 0, and the formula is set to 0 = [].
Then [] for "", the formula is for "0 =""
Then, convert "" to 0, and the formula is set to 0 = 0.
So! [] = [] To get true
Example 6:
! {}= {}
First! {} Is designed for "false". The rule is the same as "5", and the formula is converted to "false == {}
False is set to 0, and the formula is converted to 0 == {}
Perform toPrimitive on {} to get "[object Object]". The formula is designed for 0 = "[object Object]".
Perform toNumber on "[object Object]" to get NaN. The formula is set to 0 = NaN.
So! {}={} Returns false.