Why is isNaN(null) == false in JS?

后端 未结 8 1375
迷失自我
迷失自我 2020-11-28 05:07

This code in JS gives me a popup saying \"i think null is a number\", which I find slightly disturbing. What am I missing?

相关标签:
8条回答
  • 2020-11-28 06:02

    Note:

    "1" == 1 // true
    "1" === 1 // false
    

    The == operator does type-conversion, while === does not.

    Douglas Crockford's website, a Yahoo! JavaScript evangelist, is a great resource for stuff like this.

    0 讨论(0)
  • 2020-11-28 06:10

    I'm not exactly sure when it comes to JS but I've seen similar things in other languages and it's usually because the function is only checking whether null is exactly equal to NaN (i.e. null === NaN would be false). In other words it's not that it thinks that null is in fact a number, but it's rather that null is not NaN. This is probably because both are represented differently in JS so that they won't be exactly equal, in the same way that 9 !== '9'.

    0 讨论(0)
提交回复
热议问题