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 使用双等号比较时是相等的这点很有用 。
推荐阅读
-
-
闲鱼有话说|凯特不愧是未来王后,站婆婆身边一脸霸气,同穿黑礼服气场强太多包臀套装开衩大衣拖地裤西装
-
【动力】成都车展:弗迪动力将发布新1.5T高功率动力总成
-
-
-
-
融资|融资数千万,挤爆海底捞太二!这家炸鸡凭什么?
-
布拉德·皮特|布拉德·皮特正在热恋中!新欢保持好身材饮食配方曝光
-
-
-
「倾听雨落VIP」硬核科技标杆Pro再进化这才是青春的味道
-
-
『萌宠大机密』在买之前应该知道的5件事,2020款川崎Ninja1000SX
-
Fashionlog|时尚真是一场轮回,麻麻年轻时的潮流,今天看品味依旧在线
-
【长沙市第四医院医疗宣传】创新患教课堂,医患一家亲
-
-
木莲妈妈|“四技巧”防止宝宝出糗,“五招”搞定熊孩子,十一带娃走亲访友
-
DNF心情屋|拍卖行却翻倍,DNF:换装传说武器成黑马!副本已经宣告凉凉
-
娱乐圈头条|尼泊尔再次出手,给予印度沉重打击。,对印度断水之后
-
李艾|40岁李艾有多撩人,大胆挑战“3D彩绘裙”,40岁该有的样子!