Use isNaN() function to check if a value is NaN

Description

Javascript provides the isNaN() function, which accepts a single argument, to determine if the value is "NaN."

Example


console.log(isNaN(NaN)); // 
console.log(isNaN(10)); //10 is a number 

The code above generates the following result.

Note

When a value is passed into isNaN(), isNaN() tries to convert it into a number.

Some values convert into numbers directly, such as "10" or a Boolean value. For any value that cannot be converted into a number isNaN() returns true.


console.log(isNaN(NaN));       //true
console.log(isNaN(10));        //false - 10 is a number
console.log(isNaN("10"));      //false - can be converted to number 10
console.log(isNaN("asdf"));    //true - cannot be converted to a number
console.log(isNaN(true));      //false - can be converted to number 1
/* ww w . ja  v a  2s.co m*/

When isNaN() is called on objects, the object's valueOf() method is used to determine if the returned value can be converted into a number. If not, the toString() method is called and its returned value is tested as well.

The code above generates the following result.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions