# 坑：JavaScript 中 操作符“==” 和“===” 的区别

1.

'' == '0'           // false
0 == ''             // true
0 == '0'            // true

false == 'false'    // false
false == '0'        // true

false == undefined  // false
false == null       // false
null == undefined   // true

' \t\r\n ' == 0     // true


2.

var a = [1,2,3];
var b = [1,2,3];

var c = { x: 1, y: 2 };
var d = { x: 1, y: 2 };

var e = "text";
var f = "te" + "xt";

a == b            // false
a === b           // false

c == d            // false
c === d           // false

e == f            // true
e === f           // true


3.

"abc" == new String("abc")    // true
"abc" === new String("abc")   // false


4.

5.

11.9.6 The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:

1. If Type(x) is different from Type(y), return false.
2. If Type(x) is Undefined, return true.
3. If Type(x) is Null, return true.
4. If Type(x) is not Number, go to step 11.
5. If x is NaN, return false.
6. If y is NaN, return false.
7. If x is the same number value as y, return true.
8. If x is +0 and y is −0, return true.
9. If x is −0 and y is +0, return true.
10. Return false.
11. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
12. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
13. Return true if x and y refer to the same object or if they refer to objects joined to each other (see 13.1.2). Otherwise, return false.


6.两张神图

JavaScript 类型转换

undefined "undefined" NaN false throws TypeError
null "null" 0 false throws TypeError
true "true" 1 new Boolean(true)
false "false" 0 new Boolean(false)
""(空字符串) 0 false new String("")
"1.2"(非空，数字) 1.2 true new String("1.2")
"one"(非空，非数字) NaN true new String("one")
o "0" false new Number(0)
-0 "0" false new Number(-0)
NaN "NaN" false new Number(NaN)
Infinity "Infinity" true new Number(Infinity)
-Infinity "-Infinity" true new Number(-Infinity)
1（无穷大，非零） "1" true new Number(1)
{}(任意对象) 后续讲解 后续讲解 true
[] (任意数组) "" 0 true
[9] (一个数字元素) "9" 9 true
['a'] (其他数组) 使用join() 方法 NaN true
function(){}( 任意函数) 后续讲解 NaN true
posted @ 2017-05-26 21:40  BUTTERAPPLE  阅读(386)  评论(0编辑  收藏  举报