You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is the same as Type(y), then Return the result of performing Strict Equality Comparison x === y.
[]==[];// false
If x is null and y is undefined, or x is undefined and y is null, return true.
null==undefined;// ture
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y), vice versa (反之亦然).
1=='1';// true0=='';// true 0=='0';// true
Number("") => 0
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y, vice versa.
0==false;// true1==true;// true
If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y), vice versa.
Return false.
三等比较
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is different from Type(y), return false.
1==="1";// false
If Type(x) is Undefined, return true. (按顺序执行,x 与 y 须类型一致,以下一样除了 第9)
If Type(x) is Null, return true.
If Type(x) is Number, then
a. If x is NaN, return false.
b. If y is NaN, return false.
c. If x is the same Number value as y, return true.
d. If x is +0 and y is −0, return true.
e.If x is −0 and y is +0, return true.
f. Return false.
If Type(x) is String, then
a. If x and y are exactly the same sequence of code units (same length and same code unit
at corresponding indices), return true.
b. Else, return false.
If Type(x) is Boolean, then
a. If x and y are both true or both false, return true.
b. Else, return false.
If x and y are the same Symbol value, return true.
If x and y are the same Object value, return true.
说明
面试被一连串的 考基础的双等与三等判断问的有点蒙,虽然知道
==
是抽象相等运算符,===
是严格相等运算符,但其中的比较规则并不胜了解,那么为何[] == []
为true
呢 ?其他规则是如何 ?在知乎提问中找到了答案, 感谢,一起来看一下ECMAScript 6 官方文档 。
双等比较
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y), vice versa.
Return false.
三等比较
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
总结
==
运算符有可能是在进行必要的类型转换后,才再比较。===
运算符不会进行类型转换,所以如果两个值不是相同的类型,会直接返回false。使用==
时,可能发生一些特别的事情,例如:建议是从不使用
==
运算符,除了方便与null
或undefined比较时,a == null
如果a为null或undefined将返回true。参考
The text was updated successfully, but these errors were encountered: