Equality operator (Definition)

An operator that tests for equality or not.


ECMAScript edition - 2
Property/method value type:Boolean primitive

There are two equality operators:

Equality operators deal exclusively with the test for the operands being equal to one another. They yield a true or false result and are generally considered as part of the relational operator set since they are most often used in the same circumstances.

Testing two operands for equality follows these basic rules:

If the native types of the two operands are not the same, then the values are not equal unless a numeric coercion yields equal values from both sides or the operands both yield null/undefined values.

If the type of the left operand is undefined or null, it is assumed to be equal to the right operand.

If either of the operands is NaN, then they are assumed to be not equal.

Positive and negative zero are equal values.

Boolean values must be identical values to be considered equal.

Two strings of the same length containing the same character sequence (identical copies of one another in terms of Unicode character code points) are assumed to be equal.

References to the same object test as equal. References to two objects containing the same property values, even though they may be copies of one another, are not equal.

Comparing different types of operands can use coercion techniques to force the comparison to be conducted according to string, numeric or Boolean rules.

If one of the operands is an object and the other is not, then the object is converted to a primitive.

String comparisons can be by concatenating values of other types to an empty string. It may help to use the parentheses to guarantee that precedence is established as you intend it to be. Here is a forced string comparison:

("" + a) == ("" + b)

Numeric comparisons can be forced by subtracting zero. Again, grouping operators help to establish the desired precedence:

(a - 0) == (b - 0)

Boolean comparisons can be forced by performing a logical NOT on both operands, in which case precedence control with grouping operators may not be as necessary as it is with the addition and concatenation operators:

!a == !b

The values null and undefined are generally considered to be equal although they are distinctly different values. This is because early implementations did not support an explicit undefined value and allowed for it to have the same meaning as the null value.

Comparing strings is done simply according to the Unicode character code point values and does not take into account any of the more subtle semantic meanings of those characters as defined in the Unicode version 2.0 specification.

Refer to the identity operators for a more exact comparison taking data type into account.


See also:= (Assign), Associativity, Equal to (==), Equality expression, Identically equal to (===), NOT Equal to (!=), NOT Identically equal to (!==), Operator, Operator Precedence, Relational operator


ECMA 262 edition 2 - section - 11.9

ECMA 262 edition 3 - section - 11.9