avatarDr. Derek Austin 🥳

Summary

This article explains how to check for NaN (Not-a-Number) in JavaScript.

Abstract

The article explains that NaN is a special value in JavaScript that represents the result of a mathematical calculation that cannot be represented as a meaningful number. It can show up when Math functions fail or when a function trying to parse a number fails. The article also explains that NaN poisons all other math functions, leading to all other math operations resulting in NaN. The article then goes on to explain how to check for NaN in JavaScript, including using self-equality and the Object.is() method.

Opinions

  • The article explains that checking for NaN in JavaScript can be harder than it seems because the typeof NaN is "number" and NaN is unequal to every other value in JavaScript.
  • The article recommends using self-equality or the Object.is() method to check for NaN in JavaScript.
  • The article notes that modern JavaScript has an implementation to check for NaN called Number.isNaN() that works as expected.
  • The article also notes that the global isNaN() function is an older implementation whose actual purpose is to check whether a value cannot be coerced to a number.
  • The article concludes by providing additional resources for learning more about NaN in JavaScript.

How to Check for NaN in JavaScript

Checking for NaN (“not a number”) is as simple as checking for self-equality in JavaScript.

Photo by Rattap on Unsplash

In JavaScript, the special value NaN (meaning “not a number”) is used to represent the result of a mathematical calculation that cannot be represented as a meaningful number. — Joshua Clanton on A Drip of JavaScript

The special value NaN shows up in JavaScript when Math functions fail (Math.sqrt(-37)) or when a function trying to parse a number fails (parseInt("No integers here")).

NaN then poisons all other math functions, leading to all other math operations resulting in NaN.

Photo by Chris Barbalis on Unsplash

Division by zero

Note that in JavaScript, division by 0 returns Infinity, not NaN:

This result is because of how floating-point is defined, more generally than just Javascript. Why? Roughly, because 1/0 is the limit of 1/x as x approaches zero. And 0/0 has no reasonable interpretation at all, hence NaN.

What is NaN anyway?

NaN is a property of the global object. The initial value of NaN is Not-A-Number — the same as the value of Number.NaN. — MDN Docs

Photo by Andrew Buchanan on Unsplash

Checking for NaN is harder than it seems

Unfortunately, there are two problems with trying to check for NaN:

  1. The typeof NaN is “number”
  2. NaN is unequal to every other value in JavaScript

Check out this code example:

So how do we check whether we have a NaN value that will poison math?

Photo by Wolfgang Rottmann on Unsplash

Check for NaN with self-equality

“NaN, and only NaN, will compare unequal to itself.” — MDN Docs

In JavaScript, the best way to check for NaN is by checking for self-equality using either of the built-in equality operators, == or ===.

Because NaN is not equal to itself, NaN != NaN will always return true.

Of course, such a NaN test in your code is not always readable, so it is a good idea to use a comment or to create a wrapper function:

It doesn’t make a difference if you use >!= or !== to check for NaN.

Photo by Erik Mclean on Unsplash

Check for NaN with Object.is()

“The Object.is() method determines whether two values are the same value.” — MDN Docs

Unlike the strict and loose equality operators, the ES6 helper method Object.is() does not consider NaN to be equal to itself:

So, if you swap out === for Object.is(), you never have to worry about checking for NaN any special way. Problem solved!

I wrote about how to use Object.is() in JavaScript in another article:

One note about terminology — Object.is() is a function on the global Object class, I don’t call it an “equality operator” like == or ===.

For example, in Jest tests, the method .toBe() that uses === is different from the method .toEqual() that uses Object.is().

To wrap up here, I should mention that a JavaScript ES6 Set, which is used for finding unique values, will only ever keep one copy of a NaN value.

Photo by Tony Hand on Unsplash

A word about Number.isNan()

Modern JavaScript already has an implementation to check for NaN called Number.isNan() that works how you think it would. For example:

Note that Number.isNan() is different from the global isNan() function, which is an older implementation whose actual purpose is to check whether a value cannot be coerced to a number.

Here’s the difference, for completeness:

  • isNaN() will return true if the value is currently NaN, or if it is going to be NaN after it is coerced to a number. In other words, if it receives a value that can be coerced to a number, isNaN() will return false.
  • Number.isNaN() will return true only if the value is currently NaN.

So, if you are supporting old browsers (especially Internet Explorer) that don’t support Number.isNan(), then it is best to check for self-equality.

Photo by Nick Hillier on Unsplash

Additional resources:

Dr. Derek Austin is the author of Career Programming: How You Can Become a Successful 6-Figure Programmer in 6 Months, now available on Amazon.

JavaScript
Programming
Software Engineering
Data Science
Technology
Recommended from ReadMedium