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
?
SLaks
It's supposed to be symmetric. However, there is an asymmetric case in some versions of IE:
window == document; // true
document == window; // false
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 toB == A
, except in the order of evaluation ofA
andB
.
来源:https://stackoverflow.com/questions/5669440/is-javascripts-double-equals-always-symmetric