这怎么可能是假的?
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?