javascript – NaN!== parseInt(undefined);

前端之家收集整理的这篇文章主要介绍了javascript – NaN!== parseInt(undefined);前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这怎么可能是假的?
console.log(parseInt(undefined));
//NaN
console.log(parseInt(undefined)===NaN);
//false

这看起来很愚蠢

解决方法

NaN不等于任何东西,甚至本身.使用 isNaN检测NaN而不是相等.
NaN === NaN  // -> false
isNaN(NaN)   // -> true (argument is coerced [ToNumber] as required)
x = NaN
x !== x      // -> true (would be false for any other value of x)
NaN || "Hi"  // -> "Hi" (NaN is a false-y value,but not false)

这是遵循IEEE-754的JavaScript和(安静)NaN lack-of-ordering behavior的结果:

A comparison with a NaN always returns an unordered [not equal] result even when comparing with itself.

另见What is the rationale for all comparisons returning false for IEEE754 NaN values?

原文链接:https://www.f2er.com/js/159809.html

猜你在找的JavaScript相关文章