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 使用双等号比较时是相等的这点很有用 。
推荐阅读
-
跑步|很多人喜欢跑步,但是夏天跑步不利于健康?一定注意这几点
-
跟随乐园|不输涪陵榨菜,可好吃了,腌菜开胃又爽口!在国内有这3种地方菜
-
-
-
-
-
-
-
-
-
买车先看三大件,AT/MT/CVT什么来头,选谁好?
-
-
p20上市时间与价格——华为p20国内什么时候上市?
-
快科技|骁龙875出货时间曝光:明年安卓旗舰配100W充电
-
原子之城@我国实力能摧毁多少航母?俄专家:至少1艘,美军一旦发动海战
-
价格|[快讯]农业农村部:预测不会出现猪肉价格大幅上涨的情况
-
环球时报|微软内部调查显示六成微软员工反对收购TikTok
-
7月17日~23日星座运势:白羊、金牛、双子、巨蟹、狮子、处女座
-
-
蘑菇汤@这道鸡胸肉蘑菇浓汤我常做,低脂低卡,好喝到舔盘