Search code examples
javascriptecma262comparison-operatorsequivalencesymmetric

Is JavaScript's double equals (==) always symmetric?


There are many cases in which JavaScript's type-coercing equality operator is not transitive. For example, see "JavaScript equality transitivity is weird."

However, are there any cases in which == isn't symmetric? That is, where a == b is true and b == a is false?


Solution

  • In Javascript, == is always symmetric.

    The spec says:

    NOTE 2 The 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.