Javascript 如何测试 NaN?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11354373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:36:15  来源:igfitidea点击:

How do I test for NaN?

javascriptif-statementnan

提问by I wrestled a bear once.

Possible Duplicate:
Comparing NaN values for equality in Javascript

可能的重复:
比较 Javascript 中的 NaN 值是否相等

Can anyone tell me why this is not working?

谁能告诉我为什么这不起作用?

if(inbperr == NaN) {
    document.getElementById('inbclo').value = "N/A";
}
else {
    document.getElementById('inbclo').value = "%" + inbperr;
}

Instead of returning a percentage value, or "N/A", I want it to return "%NaN".

我希望它返回“%NaN”,而不是返回百分比值或“N/A”。

回答by Ned Batchelder

NaN's are unusual: they are not equal to anything, even themselves. You need to use isNaN(inbperr)to tell whether a value is a NaN or not.

NaN's 是不寻常的:他们不等于任何东西,甚至他们自己。您需要使用isNaN(inbperr)来判断一个值是否为 NaN。

回答by CoR

NaN is Not a Number. One of few JavaScript toxic types. It can reduce whole expression to NaN.

NaN 不是数字。少数 JavaScript 有毒类型之一。它可以将整个表达式简化为 NaN。

http://www.crockford.com/javascript/encyclopedia/

http://www.crockford.com/javascript/encyclopedia/