Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell whether value is NaN without using isNaN, which has false positives? [duplicate]

How can I check whether the input value is NaN or not without using the isNaN function?

like image 666
JIMMY Avatar asked Apr 10 '12 18:04

JIMMY


1 Answers

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.

like image 162
Kobi Avatar answered Sep 28 '22 04:09

Kobi