Why false == “false” is false?

前端 未结 4 1181
一个人的身影
一个人的身影 2021-02-15 14:24

I am still learning the basics of javaScript and I don\'t understand why this happens.

Having type coercion false == \"false\"would be converted into:

4条回答
  •  渐次进展
    2021-02-15 15:14

    You've misunderstood the type conversion rules. false doesn't get converted to a string before comparison.

    If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

    false is converted to a number, which gives:

    +0 == "false"
    

    … then …

    If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

    "false" is converted to a number, which gives:

    +0 == NaN
    

    … which is false.

提交回复
热议问题