This question already has an answer here:
How can I check whether the input value is NaN
or not without using the isNaN
function?
If you can use ECMAScript 6, you have Object.is
:
return Object.is(obj, NaN);
Otherwise, here is one option, from the source code of underscore.js:
// Is the given value `NaN`?
_.isNaN = function(obj) {
// `NaN` is the only value for which `===` is not reflexive.
return obj !== obj;
};
Also their note for that function:
Note: this is not the same as the native isNaN function, which will also return true if the variable is undefined.
Rob W
Convert the input to a number, and check whether the substraction is not zero:
var x = 'value';
var is_NaN = +x - x !== 0; // The + is actually not needed, but added to show
// that a number conversion is made.
来源:https://stackoverflow.com/questions/10094738/how-to-tell-whether-value-is-nan-without-using-isnan-which-has-false-positives