Javascript isNaN()

Introduction

The Javascript isNaN() function checks whether a value is NaN or not.

It returns true if the given value is NaN; otherwise, false.

isNaN(value)
ParametersMeaning
value The value to be tested.

Number.isNaN(x) is a reliable way to test whether x is NaN or not.

let a = isNaN(NaN);// true
console.log(a);//from   w w  w.j ava  2  s  . c o  m
a = isNaN(undefined); // true
console.log(a);
a = isNaN({});        // true
console.log(a);

a = isNaN(true);      // false
console.log(a);
a = isNaN(null);      // false
console.log(a);
a = isNaN(42);        // false
console.log(a);

// strings
a = isNaN('42');      // false: "42" is converted to the number 42 which is not NaN
console.log(a);
a = isNaN('42.42');   // false: "42.42" is converted to the number 42.42 which is not NaN
console.log(a);
a = isNaN("42,5");    // true
console.log(a);
a = isNaN('123ABC');  // true:  parseInt("123ABC") is 123 but Number("123ABC") is NaN
console.log(a);
a = isNaN('');        // false: the empty string is converted to 0 which is not NaN
console.log(a);
a = isNaN(' ');       // false: a string with spaces is converted to 0 which is not NaN
console.log(a);

a = isNaN(new Date());                // false
console.log(a);
a = isNaN(new Date().toString());     // true
console.log(a);

a = isNaN('asdf');   // true: Parse asdf as a number fails and returns NaN

console.log(a);



PreviousNext

Related