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 使用双等号比较时是相等的这点很有用 。
推荐阅读
-
【央视网】全国累计确诊779例,爱沙尼亚新增34例新冠肺炎确诊病例
-
-
-
-
-
青年部编版四年级语文上册第14课《普罗米修斯》课件及同步练习
-
调皮的坏老头|网友:终于可以靠脸吃饭了,两大移动支付巨头突然宣布取缔二维码
-
-
-
浮头|闽清浮头街藏着一座始建于唐代的古庙——闽清毓麟宫
-
-
北晚新视觉网|之后一幕令人意想不到,网友怒了,情侣酒驾翻车热心群众被救出
-
遇见美好数字生活,华为终端云服务带来P40系列超凡体验
-
英雄联盟|S10四强赛新痛苦面具!阿布直言这剑姬太自信
-
董事长|下滑!浩洋股份:2020年半年度净利润约6518万元,同比下降36.09%
-
-
宋慧乔|宋慧乔单身后罕见凹凸装,穿紧身裙尽显美臀,配烟熏妆差点认不出
-
[华为]有朋友问我“JAVA全栈”、“互联网架构师”和“JAVAEE”到底是什么?
-
新员工试用期工作总结(试用期工作总结怎么写 新人)
-