isNaN() vs. parseInt() confusion

后端 未结 2 1135
深忆病人
深忆病人 2021-02-12 22:47

There is something strange.

Why
with isNaN(\"\") I get False
But
with parseInt(\"\") I get NaN
?

2条回答
  •  太阳男子
    2021-02-12 23:24

    This is because "" is equivalent to zero in JavaScript. Try "" == 0. This means if you try evaluating it in a numerical equation, it will come up as 0. When you parse it on the other hand it realizes there is nothing there.

    As an alternative to parseInt you could use Math.floor. This will give you 0 for "".

提交回复
热议问题