13.11 Equality Operators
The result of evaluating an equality operator is always of type Boolean, reflecting whether the relationship named by the operator holds between its two operands.
Syntax
13.11.1 Runtime Semantics: Evaluation
- Let lref be ?
Evaluation ofEqualityExpression . - Let lval be ?
GetValue (lref). - Let rref be ?
Evaluation ofRelationalExpression . - Let rval be ?
GetValue (rref). - Return ?
IsLooselyEqual (rval, lval).
- Let lref be ?
Evaluation ofEqualityExpression . - Let lval be ?
GetValue (lref). - Let rref be ?
Evaluation ofRelationalExpression . - Let rval be ?
GetValue (rref). - Let r be ?
IsLooselyEqual (rval, lval). - If r is
true , returnfalse . Otherwise, returntrue .
- Let lref be ?
Evaluation ofEqualityExpression . - Let lval be ?
GetValue (lref). - Let rref be ?
Evaluation ofRelationalExpression . - Let rval be ?
GetValue (rref). - Return
IsStrictlyEqual (rval, lval).
- Let lref be ?
Evaluation ofEqualityExpression . - Let lval be ?
GetValue (lref). - Let rref be ?
Evaluation ofRelationalExpression . - Let rval be ?
GetValue (rref). - Let r be
IsStrictlyEqual (rval, lval). - If r is
true , returnfalse . Otherwise, returntrue .
Given 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
.
The equality operators maintain the following invariants:
-
A != B
is equivalent to!(A == B)
. -
A == B
is equivalent toB == A
, except in the order of evaluation ofA
andB
.
The 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 bothtrue . -
new String("a") == new String("a")
isfalse .
Comparison 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 normalized form.