微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/js/159809.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐