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.
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.
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
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.