Code 1:
- [] == []; // false
- [] === []; // false
- {} == {}; // SyntaxError: Unexpected token ==
- {} === {}; // SyntaxError: Unexpected token ===
Code 2:
- var n0 = 123;
- var n1 = new Number(123);
- var n2 = new Number(123);
- var n3 = Number (123);
- var n4 = Number (123);
- n0 == n1; // true
- n0 == n3; // true
- n0 === n1; // false
- n0 === n3; // true
- n1 == n2; // false
- n1 === n2; // false
- n1 == n3; // true
- n1 === n3; // false
- n3 == n4; // true
- n3 === n4; // true
Why is this result?
We need to read the specification carefully: http://ecmascript.cn/#203
11.9.3 Abstract equality comparison algorithm
Comparison operation x==y
, where the x
sum y
is the value, produces an true
OR false
. Such comparisons are made as follows:
- If it
Type(x)
is theType(y)
same, then- If
Type(x)
toUndefined
returntrue
. - If
Type(x)
toNull
returntrue
. - If
Type(x)
isNumber
, then- If
x
toNaN
returnfalse
. - If
y
toNaN
returnfalse
. - If
x
andy
are equal values, returntrue
. - If
x
it is+0
andy
is−0
to returntrue
. - If
x
it is−0
andy
is+0
to returntrue
. - Return
false
.
- If
- If it
Type(x)
isString
, it returns when thex
andy
is the same character sequence (the same length and the same character in the same position)true
. Otherwise, returnfalse
. - 若
Type(x)
为Boolean
, 当x
和y
为同为true
或者同为false
时返回true
。 否则, 返回false
。 - 当
x
和y
为引用同一对象时返回true
。否则,返回false
。
- If
- 若
x
为null
且y
为undefined
, 返回true
。 - 若
x
为undefined
且y
为null
, 返回true
。 - 若
Type(x)
为Number
且Type(y)
为String
, 返回comparison x == ToNumber(y)
的结果。 - 若
Type(x)
为String
且Type(y)
为Number
, - 返回比较
ToNumber(x) == y
的结果。 - 若
Type(x)
为Boolean
, 返回比较ToNumber(x) == y
的结果。 - 若
Type(y)
为Boolean
, 返回比较x == ToNumber(y)
的结果。 - 若
Type(x)
为String
或Number
,且Type(y)
为Object
,返回比较x == ToPrimitive(y)
的结果。 - 若
Type(x)
为Object
且Type(y)
为String
或Number
, 返回比较ToPrimitive(x) == y
的结果。 - 返回
false
。
注:按以上相等之定义:
- 字符串比较可以按这种方式强制执行:
"" + a == "" + b
。 - 数值比较可以按这种方式强制执行:
+a == +b
。 - 布尔值比较可以按这种方式强制执行:
!a == !b
。
注:等值比较操作保证以下不变:
A != B
等价于!(A==B)
。A == B
等价于B == A
,除了 A 与 B 的执行顺序。
注:相等运算符不总是传递的。 例如,两个不同的 String
对象,都表示相同的字符串值;==
运算符认为每个 String
对象都与字符串值相等,但是两个字符串对象互不相等。例如:
new String("a") == "a"
和"a" == new String("a")
皆为true
。new String("a") == new String("a")
为false
。
字符串比较使用的方式是简单地检测字符编码单元序列是否相同。不会做更复杂的、基于语义的字符或者字符串相等的定义以及 Unicode 规范中定义的 collating order。所以 Unicode 标准中认为相等的 String
值可能被检测为不等。实际上这一算法认为两个字符串已经是经过规范化的形式。
11.9.6 严格等于比较算法
比较 x===y
,x
和 y
为值,需要产出 true
或 false
。比较过程如下:
- 如果
Type(x)
与Type(y)
的结果不一致,返回false
,否则 - 如果
Type(x)
结果为Undefined
,返回true
- 如果
Type(x)
结果为Null
,返回true
- 如果
Type(x)
结果为Number
,则- 如果
x
为NaN
,返回false
- 如果
y
为NaN
,返回false
- 如果
x
与y
为同一个数字,返回true
- 如果
x
为+0
,y
为-0
,返回true
- 如果
x
为-0
,y
为+0
,返回true
- 返回
false
- 如果
- 如果
Type(x)
结果为String
,如果x
与y
为完全相同的字符序列(相同的长度和相同的字符对应相同的位置),返回true
,否则,返回false
- 如果
Type(x)
结果为Boolean
,如果x
与y
都为true
或false
,则返回true
,否则,返回false
- 如果
x
和y
引用到同一个Object
对象,返回true
,否则,返回false
注:此算法与 SameValue 算法在对待有符号的零和 NaN 上表现不同。