Check if a value is NaN

Darren Jones
May 6, 2021

NaN stands for ‘Not a Number’ and is a property of the global object that is returned if you try to perform a numerical operation that doesn’t result in a real number (such as 0/0 or Math.sqrt(-1)), or try to perform a numerical operation on a string that is not addition, for example ‘hello’/’world’.

Bizarrely, given its name, the type of NaN is  ‘number’ :

copy code
javascript
typeof NaN
// “number”

NaN is the only value in JavaScript that is not equal to itself.:

copy code
javascript
NaN === NaN
// false

There are 3 ways to check if a value is NaN:

copy code
javascript
//Use the `Number.isNaN()` method:
Number.isNaN(value)

//Use the `Object.is()` method to compare the value to NaN:
Object.is(NaN,value)

//Check if the value doesn’t equal itself:
value !== value
//(remember NaN is the only value in JavaScript that this is true for)


Know a better answer? Join our our community and let us know.