为什么(~(1<<31;转换为字符串时t相等

Why does (~(1 << 31)) === ~(1 << 31) yet they aren't equal when converted to strings?

本文关键字:lt 字符串 相等 为什么 转换      更新时间:2023-09-26

在JS中:

(~(1 << 31)) === ~(1 << 31)
> true
(~(1 << 31)).toString(2) === ~(1 << 31).toString(2)
> false

这怎么可能?我认为===运算符是相同实体之间的严格比较?

事实证明,由于JS:中的操作顺序,我没有比较确切的值

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

成员访问.toString()优先于按位运算符。哇!

这是因为表达式的求值方式如下:

(~(1 << 31)).toString(2) === ~((1 << 31).toString(2))
//----------------------------^ see the parenthesis