Javascript Number isNaN()

Introduction

The Javascript Number.isNaN() method checks whether the Number is NaN.

It is a more robust than global isNaN().

Number.isNaN(value)
Parameter Optional Meaning
value Required The value to be tested for NaN.

It returns true if the given value is NaN and its type is Number; otherwise, false.

Unlike global isNaN() function, Number.isNaN() doesn't convert the parameter to a number.

let a = Number.isNaN(NaN);        // true
console.log(a);/* www . ja  v  a  2  s  .  com*/
a = Number.isNaN(Number.NaN); // true
console.log(a);
a = Number.isNaN(0 / 0);      // true
console.log(a);
// would have been true with global isNaN()
a = Number.isNaN('NaN');      // false
console.log(a);
a = Number.isNaN(undefined);  // false
console.log(a);
a = Number.isNaN({});         // false
console.log(a);
a = Number.isNaN('asdf');   // false
console.log(a);

// These all return false
a = Number.isNaN(true);
console.log(a);
a = Number.isNaN(null);
console.log(a);
a = Number.isNaN(42);
console.log(a);
a = Number.isNaN('42');
console.log(a);
a = Number.isNaN('42.42');
console.log(a);
a = Number.isNaN('');
console.log(a);
a = Number.isNaN(' ');
console.log(a);



PreviousNext

Related