Posted almost 8 years ago. Visible to the public.

JavaScript: How to check if an object is NaN

JavaScript's NaN ("Not a Number") is hard to compare against. It never equals anything, not even itself:

NaN === NaN; // false Number.NaN === NaN; // false

There is the isNaN method, but it is not really what you are looking for:

isNaN(NaN) // true isNaN('hello') // true

Option 1: ES6

The method determines whether two values are the same value. It even works for NaN:

Copy, NaN) // true

Option 2: ES5

The example above shows that simply using isNaN would match other objects, too. However, look at the weird type of "Not a Number":

typeof NaN // "number"

Hence we can actually check against NaN like this:

var something = NaN; typeof something === 'number' && isNaN(something) // true


makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Arne Hartherz
Last edit:
almost 3 years ago
by Johanna Schalk
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more