Javascript - NaN (Not a number)

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript) > ECMAScript - Grammar Standard

1 - About

Advertising

3 - 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

4 - 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

4.1 - Utility function isReallyNaN

function isReallyNaN(x) {
    return x !== x;
}
 
console.log(isReallyNaN(NaN));
console.log(isReallyNaN("bar"));

Advertising

5 - Documentation / Reference

web/javascript/es/nan.txt · Last modified: 2019/02/16 17:40 by gerardnico