11.9.6 严格相等比较算法当比较 x===y 时 , x 与 y 为值 , 表达式返回 true 或 false , 表达式执行方式如下:1. 如果 Type(x) 和 Type(y) 不同, 返回 false.2. 如果 Type(x) 为 Undefined, 返回 true.3. 如果 Type(x) 为 Null, 返回 true.4. 如果 Type(x) 为数字,那么→ a. 如果 x 为 NaN, 返回 false.→ b. 如果 y 为 NaN, 返回 false.→ c. 如果 x 与 y 的数字值相等, 返回 true.→ d. 如果 x 为 +0 而 y 为 −0, 返回 true.→ e. 如果 x 为 −0 而 y 为 +0, 返回 true.→ f. 返回 false.5. 如果 Type(x) 为字符串,如果 x 与 y 中的字符顺序完全相同(长度相同 , 字符位置相同) , 则返回 true;否则返回 false.6. 如果 Type(x) 为布尔类型, 如果 x 与 y 皆为真或假 , 则返回 true;否则 , 返回 false.7. 如果 x 和 y 引用了同一个对象 , 返回 true;否则返回 false.NOTE — This algorithm d如果fers from the SameValue Algorithm (9.12) in its treatment of signed zeroes and NaNs.
标准相等运算符
标准相等运算符(== 和 !=)使用抽象相等比较算法来比较两个运算数:
11.9.3 抽象相等比较算法(Abstract Equality Comparison Algorithm)比较 x == y, x 和 y 为值, 表达式返回 true 或 false. 表达式执行如下:1. 如果 Type(x) 与 Type(y) 相同, 那么→ a. 如果 Type(x) 为 Undefined, 返回 true.→ b. 如果 Type(x) 为 Null, 返回 true.→ c. 如果 Type(x) 为数字, 则→ → i. 如果 x 为 NaN, 返回 false.→ → ii. 如果 y 为 NaN, 返回 false.→ → iii. 如果 x 与 y 数字值相等, 返回 true.→ → iv. 如果 x 为 +0 并且 y 为 −0, 返回 true.→ → v. 如果 x 为 −0 并且 y 为 +0, 返回 true.→ → vi. 返回 false.→ d. 如果 Type(x) 是字符串, 如果 x 与 y 中的字符顺序完全相同(长度相同 , 字符位置相同) , 则返回 true;否则返回 false.→ e. 如果 Type(x) 为布尔类型, 如果 x 与 y 皆为真或假 , 则返回true;否则返回 false.→ f. 如果 x 与 y 引用了同一个对象 , 返回 true;否则返回 false.2. 如果 x 为 null 并且 y 为 undefined, 返回 true.3. 如果 x 为 undefined 并且 y 为 null, 返回 true.4. 如果 Type(x) 为数字并且 Type(y) 为字符串, 返回 x == ToNumber(y) 的结果.5. 如果 Type(x) 为字符串并且 Type(y) 为数字, 返回 ToNumber(x) == y 的结果.6. 如果 Type(x) 为布尔类型, 返回 ToNumber(x) == y 的结果.7. 如果 Type(y) 为布尔类型, 返回 x == ToNumber(y) 的结果.8. 如果 Type(x) 为数字或字符串 , 并且 Type(y) 为 Object, 返回 x == ToPrimitive(y) 的结果.9. 如果 Type(x) 为 Object 并且 Type(y) 为数字或字符串, 返回 ToPrimitive(x) == y 的结果.10. 返回 false.NOTE 1 — Given the above definition of equality:• String comparison can be forced by: "" + a == "" + b.• Numeric comparison can be forced by: +a == +b.• Boolean comparison can be forced by: !a == !b.NOTE 2 — The equality operators maintain the following invariants:• A != B is equivalent to !(A == B).• A == B is equivalent to B == A, except in the order of evaluation of A and B.NOTE 3 The equality operator is not always transitive. For example, there might be two distinct String objects, each representing the same String value; each String object would be considered equal to the String value by the == operator, but the two String objects would not be equal to each other. For example:• new String("a") == "a" and "a" == new String("a")are both true.• new String("a") == new String("a") is false.NOTE 4 Comparison of Strings uses a simple equality test on sequences of code unit values. There is no attempt to use the more complex, semantically oriented definitions of character or string equality and collating order defined in the Unicode spec如果ication. Therefore Strings values that are canonically equal according to the Unicode standard could test as unequal. In effect this algorithm assumes that both Strings are already in normalized form.
结论
通常来说 , 我比较喜欢用 === 和 !== , 除非碰到了必须使用 == 和 != 的情况 , 比如检查空值 。
另外 , 在检查空值时 , 牢记 null 和 undefined 使用双等号比较时是相等的这点很有用 。
推荐阅读
-
请问15岁的女生吃富马酸喹硫平接近一年,每天吃四片的富马酸喹硫平片,对我的大脑和神经会有影响吗
-
-
华为华为P50最新确认,一体全视屏+鸿蒙OS+双8500万,这才是华为
-
文史漫今生少年成名诗传长安,为何老来落寞,诗仙李白奋斗史
-
充电宝■同样是共享模式,为什么只有共享充电宝活的滋润?
-
-
『大众CC』吉利全新旗舰车型!1.5T榨出180马力,油耗5.8L,比大众CC时尚
-
-
在高速上行车,碰到这4种标线一定要当心,交警:第四种12分扣完
-
『地区气温』今日又将升温!重庆各地最高气温普遍超30℃
-
-
-
外卖|外卖小哥闯红灯被撞倒,深圳交警提醒骑手遵守交通法规
-
-
医院|津巴布韦首家“中国标准”新冠诊疗医院投入使用
-
北京经信局潘锋:利用区块链技术建金融专区,解决小微企业贷款难
-
-
-
-