我仍在学习 javaScript 的基础知识,我不明白为什么会这样。
具有类型强制 false == "false"
将被转换为:
false == false //true
或
"false" == "false" //true
那么,为什么 false == "false"
是假的?
最佳答案
你误解了 type conversion rules . false
在比较之前不会转换为字符串。
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
false
转换为数字,给出:
+0 == "false"
……然后……
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
… "false"
被转换为一个数字,给出:
+0 == NaN
…这是错误的。
关于javascript - 为什么 false == "false"是假的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38814212/