Appearance
比较值相等
==
如果 Type(x) 和 Type(y) 相同,返回 x === y 的结果。
如果 x 是 null 且 y 是 undefined,返回 true。
如果 x 是 undefined 且 y 是 null,返回 true。
如果 Type(x) 是 Number 且 Type(y) 是 String,返回 x == toNumber(y) 的比较结果。
如果 Type(x) 是 String 且 Type(y) 是 Number,返回 toNumber(x) == y 的比较结果。
如果 Type(x) 是 Boolean,返回 toNumber(x) == y 的比较结果。
如果 Type(y) 是 Boolean,返回 x == toNumber(y) 的比较结果。
如果 Type(x) 是 String 或 Number 或 Symbol 且 Type(y) 是 Object,返回 x == toPrimitive(y) 的比较结果
如果 Type(x) 是 Object 且 Type(y) 是 String 或 Number 或 Symbol,返回 toPrimitive(x) == y 的比较结果
返回 false。
javascript
null == undefined // output: true
NaN == NaN // output: false
NaN != NaN // output: true
Infinity == Infinity // output: true
Infinity == Infinity + 1 // output: true
Symbol(1) == Symbol(1) // output: false
const s1 = Symbol(2)
const s2 = s1
s1 == s2 // output: true
{} == {} // output: false
[10] == '10' // true
{} == '{}' // false
1 == true // true
2 == true // false
-1 == false // false
0 == false // true
1 == '1' // true
true == '1' // true
false == '' // true
[] == 0 // true
null == 0 // false===
如果 Type(x) 和 Type(y) 是不同的,返回 false。
如果 Type(x) 是 Undefined,返回 true。
如果 Type(x) 是 Null,返回 true。
如果 Type(x) 是 Number
如果 x 是 NaN,返回 false。
如果 y 是 NaN,返回 false。
如果 x 和 y 是 相同的数字值,返回 true。
如果 x 是 +0 且 y 是 -0,返回 true。
如果 x 是 -0 且 y 是 +0,返回 true。
返回 false
如果 Type(x) 是 String
如果 x 和 y 是完全相同的代码单元序列(相同的长度和在对应的索引有相同的代码单元),返回 true。
否则,返回 false。
如果 Type(x) 是 Boolean
如果 x 和 y 都为 true 或 都为 false,返回 true。
否则,返回 false。
如果 x 和 y 是相同的 Symbol value,返回 true。
如果 x 和 y 是相同的 Object value,返回 true。
返回 false。
Object.is()
javascript
// Object.is() 和 === 的不同之处在于它对于有符号零和 NaN 的处理
Object.is(NaN, NaN) // output: true
Object.is(0, 0) // output: true
Object.is(+0, +0) // output: true
Object.is(-0, -0) // output: true
Object.is(+0, -0) // output: false
Object.is(-0, +0) // output: false
+0 === +0 // true
-0 === -0 // true
+0 === -0 // true
-0 === +0 // true