Posted about 5 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:

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

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

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

Option 1: ES6

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

Copy
Object.is(NaN, 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":

Copy
typeof NaN // "number"

Hence we can actually check against NaN like this:

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

Wow.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Avatar
Arne Hartherz
Last edit:
about 1 month ago
by Johanna Hartmann
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 cookies to improve usability and analyze traffic.
Accept or learn more