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

后端 未结 8 1427
迷失自我
迷失自我 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

    I just ran into this issue myself.

    For me, the best way to use isNaN is like so

    isNaN(parseInt(myInt))

    taking phyzome's example from above,

    var x = [undefined, NaN,     'blah', 0/0,  null, 0,     '0',   1,     1/0, -1/0,  Number(5)]
    x.map( function(n){ return isNaN(parseInt(n))})
            [true,      true,    true,   true, true, false, false, false, true, true, false]
    

    ( I aligned the result according to the input, hope it makes it easier to read. )

    This seems better to me.

提交回复
热议问题