37

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?

Community
  • 1
  • 1
Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196

3 Answers3

37

It's supposed to be symmetric. However, there is an asymmetric case in some versions of IE:

window == document; // true
document == window; // false
Jason LeBrun
  • 13,037
  • 3
  • 46
  • 42
30

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.
Damiano
  • 791
  • 1
  • 8
  • 22
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
11

The answer to your actual question (is the operator symmetric) is yes. The ECMA-262 spec explicitly states:

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.
Raph Levien
  • 5,088
  • 25
  • 24