How do I test for NaN? [duplicate]

2019-01-18 08:21发布

问题:

Possible Duplicate:
Comparing NaN values for equality in Javascript

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" like I want it to it's returning "%NaN".

回答1:

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.



回答2:

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

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