isNaN() global function JS Home  <<  JS Reference  <<  isNaN()

You cannot rely on the equality (==) and strict equality (===) comparison operators of the Nan global property to find out whether a value is Nan or not

For this reason we have the isNaN() global function to test for Nan.

  • Be aware that some results using the isNaN() function are unreliable as demonstrated in the example, so use this function with care.

Description

The isNaN global function is used to determine whether a value is NaN (Not-A-Number).

Syntax

Signature Description
isNaN(testValue)Global function is used to determine whether a value is NaN (Not-A-Number).

Parameters

Parameter Description
testValueThe value to be tested.

Below are some examples showing usage of the isNaN() function.

// isNaN.
var isNaNValues = new Array(5);

isNaNValues[0] = isNaN(17); // number so false

isNaNValues[1] = isNaN('17'); // string number so false

isNaNValues[2] = isNaN(17.2345); // float number so false

isNaNValues[3] = isNaN(NaN); // isNaN is NaN so true

*/
 / The next test shows why isNan is unreliable.
 / 1) Parsing 'aaa' as a number fails and so Nan is returned
 / 2) As you can see from the example above isNaN(NaN) returns true
 / 3) Therefore parsing a string that cannot be converted to a numeric gives a 
 /    double positive and makes isNaN() unreliable in this situation.
 /*

isNaNValues[4] = isNaN('aaa');
alert(isNaNValues);

Press the button below to action the above code:

Related Tutorials

JavaScript Intermediate Tutorials - Lesson 6 - More Maths Functions