为什么 0 == ""
在 JavaScript 中为真?我找到了一个 similar post here ,但为什么数字 0 类似于一个空字符串?当然,0 === ""
是错误的。
最佳答案
0 == ''
左操作数是数字类型。
右操作数是字符串类型。
在这种情况下,右操作数被强制转换为 Number 类型:
0 == Number('')
结果
0 == 0
来自抽象相等比较算法(第 4 条):
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
关于javascript - 为什么 0 == ""在 JavaScript 中为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7627342/