开发者

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

开发者 https://www.devze.com 2023-02-25 03:03 出处:网络
There are many cases in which JavaScript\'s type-coercing equality operator is not transitive. For example, see \"JavaScript equality tran开发者_运维问答sitivity is weird.\"

There are many cases in which JavaScript's type-coercing equality operator is not transitive. For example, see "JavaScript equality tran开发者_运维问答sitivity is weird."

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


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

window == document; // true
document == window; // false


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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号