javascript比较运算符! != vs !==

Javascript Comparison Operators != vs !==

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Difference between == and === in JavaScript

我有两个变量要比较。

结果不应该相等,在哪种情况下我需要使用!=和!=?

因为当我使用这两个操作符时,它都能正常工作,但我需要知道到底有什么区别。


关于它们差异的人类可读文本

使用!=======!=更严格。前者将检查正在比较的对象是否属于同一类型,以及值是否匹配。

使用==将使隐式强制转换成为可能,请参见下面的示例。

1
2
3
4
5
(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

标准怎么说?

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.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
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    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

  • 不同之处在于前者(!=版本)将强制两个变量在比较之前类型兼容。因此:

    1
    2
    "" == 0    -> true
    "" === 0   -> false

    另一个版本要求严格相等-这两个值必须都是同一类型且具有相同的值。大多数时候这是你应该实际使用的。

    在对象的情况下,严格的相等意味着它们实际上是同一个对象。对象之间的比较不执行对象内容的逐字段比较。

    更多信息请参见https://developer.mozilla.org/en/javascript/reference/operators/comparison_operators。


    区别很简单:!==仅当变量具有相同类型且相等时返回。