11.9.1The Equals Operator ( == )

The production EqualityExpression : EqualityExpression == RelationalExpression is evaluated as follows:

Let lref be the result of evaluating EqualityExpression.

Let lval be GetValue(lref).

Let rref be the result of evaluating RelationalExpression.

Let rval be GetValue(rref).

Return the result of performing abstract equality comparison rval == lval. (see 11.9.3).

11.9.2The Does-not-equals Operator ( != )

The production EqualityExpression : EqualityExpression != RelationalExpression is evaluated as follows:

Let lref be the result of evaluating EqualityExpression.

Let lval be GetValue(lref).

Let rref be the result of evaluating RelationalExpression.

Let rval be GetValue(rref).

Let r be the result of performing abstract equality comparison rval == lval. (see 11.9.3).

If r is true, return false. Otherwise, return true.

11.9.3The 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

If Type(x) is Undefined, return true.

If Type(x) is Null, return true.

If Type(x) is Number, then

If x is NaN, return false.

If y is NaN, return false.

If x is the same Number value as y, return true.

If x is +0 and y is −0, return true.

If x is −0 and y is +0, return true.

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.

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.

NOTE 1Given 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 2The 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 3The 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 4Comparison 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 specification. 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 normalised form.

11.9.4The Strict Equals Operator ( === )

The production EqualityExpression : EqualityExpression === RelationalExpression is evaluated as follows:

Let lref be the result of evaluating EqualityExpression.

Let lval be GetValue(lref).

Let rref be the result of evaluating RelationalExpression.

Let rval be GetValue(rref).

Return the result of performing the strict equality comparison rval === lval. (See 11.9.6)

The Strict Does-not-equal Operator ( !== )

The production EqualityExpression : EqualityExpression !== RelationalExpression is evaluated as follows:

Let lref be the result of evaluating EqualityExpression.

Let lval be GetValue(lref).

Let rref be the result of evaluating RelationalExpression.

Let rval be GetValue(rref).

Let r be the result of performing strict equality comparison rval === lval. (See 11.9.6)

If r is true, return false. Otherwise, return true.

11.9.6The 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

If x is NaN, return false.

If y is NaN, return false.

If x is the same Number value as y, return true.

If x is +0 and y is −0, return true.

If x is −0 and y is +0, return true.

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.

NOTEThis algorithm differs from the SameValue Algorithm (9.12) in its treatment of signed zeroes and NaNs.

Q.E.D.