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

我与影子孤独终老i 提交于 2019-11-30 05:39:28
SLaks

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.

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 to B == A, except in the order of evaluation of A and B.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!