为什么false ==“false”是假的? [英] Why false == "false" is false?
问题描述
我还在学习javaScript的基础知识,我不明白为什么会这样。
I am still learning the basics of javaScript and I don't understand why this happens.
有类型强制 false ==false
将转换为:
false == false //true
或
"false" == "false" //true
那么,为什么 false == false
是假的?
推荐答案
你误解了类型转换规则。 false
在比较之前不会转换为字符串。
You've misunderstood the type conversion rules. false
doesn't get converted to a string before comparison.
如果Type( x)是布尔值,返回比较结果ToNumber(x)== y。
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
false
转换为一个数字,它给出:
false
is converted to a number, which gives:
+0 == "false"
...然后......
… then …
如果Type(x)是Number而Type(y)是String,
返回比较结果x == ToNumber(y)。
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
... false
转换为一个数字,它给出:
… "false"
is converted to a number, which gives:
+0 == NaN
...这是假的。
这篇关于为什么false ==“false”是假的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!