The JavaScript language has many data types, and among them, one stands out with its interesting characteristic and often confuses developers who are either unfamiliar with JavaScript or just starting to work with the language: the NaN
type.
NaN
stands for Not a Number and is used in JavaScript to represent data types that are not valid numbers. However, characteristically, the NaN
type itself is still a number.
When you run the code block below, you will encounter an interesting result.
let x = NaN
console.log(typeof x) // number
Even though NaN
represents values that are not valid numbers, NaN
itself is considered a number. Most of the time, you will get NaN
values when you try to perform invalid mathematical operations or when you attempt to parse a string into a number and it fails.
console.log(0 / 0) // NaN
console.log(Number('abc')) // NaN
console.log(parseInt('abc')) // NaN
In different programming languages, operations like those mentioned above may throw an exception, forcing you to catch them. However, in JavaScript, you will receive a NaN
value instead of an exception. Therefore, it is a good programming pratice to always check the result of a mathematical operation.
How to check for NaN
Javascript provides two ways to check for NaN
:
isNaN()
global function
You can check whether a value is NaN
or can be converted to NaN
. However, even though this function is widely used, it has a drawback: it performs a loose check and can return true
for non-numeric strings as well.
console.log(isNaN(NaN)); // true
console.log(isNaN('abc')); // true
console.log(isNaN(undefined)); // true
console.log(isNaN('123')); // false
console.log(isNaN(123)); // false
console.log(isNaN('Hello')); // true
Number.isNaN()
method (recommended)
Another method provided by JavaScript is Number.isNaN
, which is actually recommended. It specifically checks if a value is NaN
and does not attempt to coerce the value.
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN('abc')); // false
console.log(Number.isNaN(undefined)); // false
console.log(Number.isNaN('123')); // false
console.log(Number.isNaN(123)); // false
console.log(Number.isNaN('Hello')); // false
Because Number.isNaN
directly checks whether the given value is NaN
without trying to coerce it, it provides more accurate results.
Use cases for NaN
Even though it is difficult to break down the use cases of NaN
into just two simple categories, the following scenarios are the most common use cases of NaN
.
Input validation
In cases where you need to accept input from third-party sources such as web APIs or user forms, you should validate whether the given value is a valid number before proceeding to prevent your application from crashing.
function validateNumber(input) {
const num = Number(input)
return !Number.isNaN(num)
}
console.log(validateNumber('123')) // true
console.log(validateNumber('abc')) // false
console.log(validateNumber('1.23')) // true
console.log(validateNumber('1.23.4')) // false
Error handling in calculations
NaN
is also used to handle errors in mathematical operations.
function divide(a, b) {
let result = a / b;
if (Number.isNaN(result)) {
console.log("Invalid division!");
} else {
console.log("Result:", result);
}
}
divide(0, 0); // Invalid division!
divide(10, 2); // Result: 5
Key takeaways
There are some tricky aspects of NaN
that are worth mentioning.
NaN
equality
NaN
is the only value in JavaScript that is not equal to itself.
console.log(NaN === NaN); // false
console.log(Object.is(NaN, NaN)); // true (accurate comparison)
NaN
is of type number
Even though it means Not a Number, NaN
is actually of type number
.
console.log(typeof NaN); // number
Invalid math operations result in NaN
Any invalid mathematical operations result in NaN
.
console.log(0 / 0); // NaN
console.log(parseInt("Hello")); // NaN
console.log(Math.sqrt(-1)); // NaN
Invalid number parsing operations result in NaN
Invalid string to number conversion operations result in NaN
.
console.log(Number('44')) // 4
console.log(Number('44.44')) // 44.44
console.log(Number('44,44')) // NaN
console.log(Number('44.44.44')) // NaN
Conclusion
NaN
might seem like one of JavaScript's weirdest quirks at first glance. But when you dig deeper, it makes sense. NaN
has a single purpose: to represent a value that is not a valid number, allowing you to handle errors more easily and prevent your application from crashing. As a direct implementation of the IEEE 754 floating-point standard, it provides a way to represent invalid or undefined mathematical results without causing runtime errors. Rather than halting execution when an impossible calculation occurs—like dividing zero by zero as shown above—JavaScript uses NaN
to signal that a numeric operation has failed.