Strict equality using ===
比較以前不轉換類型,
若是不一樣類型,不相等,
若是相同類型:若是兩個都不是numbers,只有本身和本身比較才相等,其餘都不相等;
若是兩個都是numbers,兩個有相同的值(都不是NaN)或者一個是 +0 另外一個是-0才相等,其餘都不相等。(The only case in which (x !== x) is true is when x is NaN.)ide
Loose equality using ==
兩個值轉換爲一個common type(一個或者兩個可能通過轉換),
後面的比較和 === 相同。spa
Operand B | |||||||
---|---|---|---|---|---|---|---|
Undefined | Null | Number | String | Boolean | Object | ||
Operand A | Undefined | true |
true |
false |
false |
false |
false |
Null | true |
true |
false |
false |
false |
false |
|
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) |
A == ToPrimitive(B) |
|
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) |
A === B |
ToNumber(A)試着在比較前轉換它的參數爲一個number,行爲等價於 +A;
ToPrimitive(A)試着在比較前轉換它的參數爲 a primitive value,試着調用A的一系列方法A.toString()和A.valueOf()。code
Same-value equality
whether two values are functionally identical in all contexts.it
x | y | == |
=== |
Object.is |
---|---|---|---|---|
undefined |
undefined |
true |
true |
true |
null |
null |
true |
true |
true |
true |
true |
true |
true |
true |
false |
false |
true |
true |
true |
'foo' |
'foo' |
true |
true |
true |
0 |
0 |
true |
true |
true |
+0 |
-0 |
true |
true |
false |
0 |
false |
true |
false |
false |
"" |
false |
true |
false |
false |
"" |
0 |
true |
false |
false |
'0' |
0 |
true |
false |
false |
'17' |
17 |
true |
false |
false |
[1, 2] |
'1,2' |
true |
false |
false |
new String('foo') |
'foo' |
true |
false |
false |
null |
undefined |
true |
false |
false |
null |
false |
false |
false |
false |
undefined |
false |
false |
false |
false |
{ foo: 'bar' } |
{ foo: 'bar' } |
false |
false |
false |
new String('foo') |
new String('foo') |
false |
false |
false |
0 |
null |
false |
false |
false |
0 |
NaN |
false |
false |
false |
'foo' |
NaN |
false |
false |
false |
NaN |
NaN |
false |
false |
true |