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 使用双等号比较时是相等的这点很有用 。
推荐阅读
-
『瑜伽』肤色暗沉无光泽?多练这6式瑜伽,肌肤变得白嫩透亮
-
何有强|大波浪卷发搭配衬衫优雅大气,年轻20岁不止,章小蕙57岁不显老
-
-
-
「执勤」【新春走基层】战风雪斗严寒恪尽职守:武警官兵雪中坚守
-
荆州农商行LPR利率转换页面,贷款人,重定价日只能选
-
丹麦|去年扑杀埋葬今年挖出来 丹麦焚烧400万水貂尸体
-
游戏音符提前的生日礼物,绝地求生蜜蜂生日会来袭!4am昨日拿下亚军
-
星空护肤知识分享|夏季皮肤总是油腻的原因是什么?怎么改善
-
人民日报海外版海外网|预计年底将达四成,研究称四分之一印度人曾感染新冠病毒
-
鲸鲸说历史曹操大败而归,孙刘联军为何没有趁机攻下许昌?,赤壁之战后
-
宏信证券|宏信、万和、中邮三券商深夜接罚单 8人被点名批评
-
人民日报客户端北京频道|?中关村科学城将建千亿级空天产业集群
-
婴儿游泳馆店庆宣传语▲2019年婴儿游泳馆最新活动宣传语曝光
-
-
?“这可怎么办??”大连男子今早看到对面楼楼顶一幕,心惊不已…
-
促醒者|看完6集《促醒者》,我连发感叹,导演真敢拍,王志文真敢演
-
-
李谈历史|到死没当上皇后,原因出在嘉庆身上!乾隆做了啥,令妃十年生六胎
-