Javascript - NaN (Not a number)

About

Number - NaN (Not A Number) in Javascript.

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





Discover More
Javascript - Boolean (Truthy|Falsy)

in Javascript. JavaScript defines a list of specific values that are considered “falsy” because when coerced to a boolean, they become false. Any other value not on the “falsy” list is automatically...
Javascript - Number (Double)

Most programming languages have several types of numeric data, but JavaScript gets away with just one. All numbers are and are stored as doubles. See also': 32 bit 53 bit - the max of double-precision...
Data System Architecture
Number - NaN (Not A Number)

NaN, or Not a Number, is a value that returns when impossible things are asked in arithmetic -- like: divide zero by zero. or during the bad coercion NaN (paradoxically named not a number) is a special...



Share this page:
Follow us:
Task Runner