Javascript - NaN (Not a number)

About

Nan is not equal to itself

The IEEE floating-point standard requires that NaN must be treated as unequal to itself.

console.log(NaN === NaN);    // false

isNaN

console.log(isNaN(NaN));    // true
  • but the below expressions are also TRUE !!
console.log(isNaN("foo"));              // true
console.log(isNaN(undefined));          // true
console.log(isNaN({}));                 // true
  • you check then a NaN value not with the isNaN function but with the fact that this is the only value not equal to itself
var foo = NaN;
console.log(foo !== foo);    // true; This is NaN
var bar = "bar";
console.log(bar !== bar);    // false; This is not a NaN

Utility function isReallyNaN

function isReallyNaN(x) {
    return x !== x;
}

console.log(isReallyNaN(NaN));
console.log(isReallyNaN("bar"));

Documentation / Reference


Powered by ComboStrap