Extension: 1.var a = [0] if (a) console.log (a == true); a = [2]; a == true; // print result is false
2. var b = 2; if (b) console.log (b == true) // print result is false
When carried out at a reaction table A == B is determined in operation, at the contents from https://developer.mozilla.org
The comparison value B | |||||||
---|---|---|---|---|---|---|---|
Undefined | Null | Number | String | Boolean | Object | ||
The comparison value A | Undefined | true |
true |
false |
false |
false |
IsFalsy(B) |
Null | true |
true |
false |
false |
false |
IsFalsy(B) |
|
Number | false |
false |
A === B |
A === ToNumber(B) |
A=== ToNumber(B) |
A=== ToPrimitive(B) |
|
String | false |
false |
ToNumber(A) === B |
A === B |
ToNumber(A) === ToNumber(B) |
ToPrimitive(B) == A |
|
Boolean | false |
false |
ToNumber(A) === B |
ToNumber(A) === ToNumber(B) |
A === B |
ToNumber(A) == ToPrimitive(B) | |
Object | false | false | ToPrimitive(A) == B |
ToPrimitive(A) == B |
ToPrimitive(A) == ToNumber(B) |
|
In the above table, ToNumber(A)
attempts before comparing to digital conversion parameters A, which is the same as A + (unary +) effect. ToPrimitive(A)
By trying to call the A A.toString()
and A.valueOf()
methods to convert the parameter A to its original value (Primitive).
In general, according to the ECMAScript specification, all the objects are undefined
and null
are not equal. But most browsers allow a very narrow class of objects (ie, all pages of document.all
the object), in some cases, act as emulate undefined
role. It is equal to the operator under such a background. Thus, IsFalsy(A)
the method is true
, if and only if A
follow undefined
. In all other cases, an object will not be equal to undefined
or null
.
1 thus understood that a conditional statement will be converted to Boolean function by true or false, and in the determination of a first valueOf method call, then call toString method, and then they are converted to Number The, it is converted to a 0, 1 is installed for the true, and later be converted to a 2,
2, the comparison value and Boolean, which are then converted into a numerical comparison type
so long as the comparison with the Number or Boolean type, will be converted to both sides of the equation Number
Congruent without any type of conversion based on the determination of whether the same