Javascript 比较运算符 != 与 !==
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8616483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Comparison Operators != vs !==
提问by manny
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Difference between == and === in JavaScript
可能的重复:
Javascript === vs ==:我使用哪个“相等”运算符重要吗?
JavaScript 中 == 和 === 的区别
I have two variables to compare.
我有两个变量要比较。
Result should not be equal, in which condition i need to use != and !== ?
结果不应该相等,在哪种情况下我需要使用 != 和 !== ?
because when i use both operator it is working properly, but i need to know exactly what is the difference.
因为当我同时使用这两个运算符时它工作正常,但我需要确切地知道有什么区别。
回答by Filip Roséen - refp
Human readable text about their differences
关于它们差异的人类可读文本
Using !==
and ===
will do a more strict compare than ==
/!=
. The former will check if the objects being compared are of the same type, as well as if the values matches.
使用!==
and===
会比==
/做更严格的比较!=
。前者将检查被比较的对象是否属于同一类型,以及值是否匹配。
Using ==
will make it possible for an implicit cast to be made, see the below examples.
使用==
将使隐式转换成为可能,请参见下面的示例。
(0 == '0') // true
(0 === '0') // false
('' == 0 ) // true, the string will implicitly be converted to an integer
('' === 0 ) // false, no implicit cast is being made
What does the standard say?
标准怎么说?
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:
- If Type(x) is different from Type(y), return false.
- If Type(x) is Undefined, return true.
- If Type(x) is Null, return true.
If Type(x) is Number, then
a. If x is NaN, return false.
b.If y is NaN, return false.
c. If x is the same Number value as y, return true.
d. If x is +0 and y is ?0, return true.
e. If x is ?0 and y is +0, return true.
f. Return false.
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.
- If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
- Return true if x and y refer to the same object. Otherwise, return false. NOTE This algorithm differs from the SameValue Algorithm (9.12) in its treatment of signed zeroes and NaNs.
11.9.6 严格相等比较
算法 比较 x === y,其中 x 和 y 是值,产生真或假。这样的比较如下:
- 如果 Type(x) 与 Type(y) 不同,则返回 false。
- 如果 Type(x) 未定义,则返回 true。
- 如果 Type(x) 为 Null,则返回 true。
如果 Type(x) 是 Number,那么
一种。如果 x 是 NaN,则返回 false。
b.如果 y 是 NaN,则返回 false。
C。如果 x 与 y 的 Number 值相同,则返回 true。
d. 如果 x 为 +0 且 y 为 ?0,则返回 true。
e. 如果 x 是 ?0 并且 y 是 +0,则返回 true。
F。返回假。
如果 Type(x) 是 String,那么如果 x 和 y 是完全相同的字符序列(相同的长度和相应位置的相同字符),则返回 true;否则,返回false。
- 如果 Type(x) 是布尔值,如果 x 和 y 都为真或都为假,则返回真;否则,返回false。
- 如果 x 和 y 引用同一个对象,则返回 true。否则,返回false。注意此算法在处理有符号零和 NaN 方面与 SameValue 算法 (9.12) 不同。
11.9.3 The Abstract Equality Comparison Algorithm
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is the same as Type(y), then
a. If Type(x) is Undefined, return t rue.
b. If Type(x) is Null, return true.
c. If Type(x) is Number, then
1. If x is NaN, return false. 2. If y is NaN, return false. 3. If x is the same Number value as y, return true. 4. If x is +0 and y is ?0, return true. 5. If x is ?0 and y is +0, return true. 6. Return false.
d. 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.
e. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false. f. Return true if x and y refer to the same object. Otherwise, return false.
- If x is null and y is undefined, return true.
- If x is undefined and y is null, return true.
- If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
- If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
- If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
- If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
- If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y) .
- If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.
- Return false
11.9.3 抽象相等比较算法
比较 x == y,其中 x 和 y 是值,产生真或假。这样的比较如下:
如果 Type(x) 与 Type(y) 相同,则
一种。如果 Type(x) 是未定义的,则返回真。
湾 如果 Type(x) 为 Null,则返回 true。
C。如果 Type(x) 是 Number,那么
1. If x is NaN, return false. 2. If y is NaN, return false. 3. If x is the same Number value as y, return true. 4. If x is +0 and y is ?0, return true. 5. If x is ?0 and y is +0, return true. 6. Return false.
d. 如果 Type(x) 是 String,那么如果 x 和 y 是完全相同的字符序列(相同的长度和相应位置的相同字符),则返回 true。否则,返回false。
e. 如果 Type(x) 是布尔值,如果 x 和 y 都为真或都为假,则返回真。否则,返回false。F。如果 x 和 y 引用同一个对象,则返回 true。否则,返回false。
- 如果 x 为空且 y 未定义,则返回 true。
- 如果 x 未定义且 y 为空,则返回 true。
- 如果 Type(x) 是 Number 并且 Type(y) 是 String,则返回比较结果 x == ToNumber(y)。
- 如果 Type(x) 是 String 并且 Type(y) 是 Number,则返回比较结果 ToNumber(x) == y。
- 如果 Type(x) 是布尔值,则返回 ToNumber(x) == y 的比较结果。
- 如果 Type(y) 是布尔值,则返回比较结果 x == ToNumber(y)。
- 如果 Type(x) 是 String 或 Number 并且 Type(y) 是 Object,则返回比较结果 x == ToPrimitive(y) 。
- 如果 Type(x) 是 Object 并且 Type(y) 是 String 或 Number,则返回比较结果 ToPrimitive(x) == y。
- 返回假
回答by Alnitak
The difference is that the former (!=
) version will coercethe two variables to be type compatible before the comparison. Hence:
不同的是,前一个 ( !=
) 版本会在比较之前强制两个变量类型兼容。因此:
"" == 0 -> true
"" === 0 -> false
The other version requires strictequality - the two values must both be of the same type and have the same value. Most of the time this is the one you should actually use.
另一个版本要求严格相等——两个值必须是相同的类型并且具有相同的值。大多数情况下,这是您实际应该使用的。
In the case of objects strict equality means that they are actually the same object. A comparison between objects does not perform a field-by-field comparison of the contentsof the object.
在对象的情况下,严格相等意味着它们实际上是同一个对象。对象之间的比较不会对对象的内容进行逐个字段的比较。
See https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operatorsfor more.
有关更多信息,请参阅https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators。
回答by bjornd
The difference is that !== returns true only when variables have the same type and are not equal.
不同之处在于 !== 仅当变量具有相同类型且不相等时才返回 true。