Do you really know everything about NaN?

Ravi Rupareliya
Simform Engineering
3 min readJun 13, 2024

NaN, oh yes, I know; it stands for Not a Number.

This is what comes to mind whenever we talk about NaN in JavaScript. But have you ever taken a deep dive into it? How does it work? What are all the possible things we can do with NaN? Can we really use it just to check if a given value is a number or not?

Let’s do a deep dive and understand it in detail.

The first and most common use case we have for NaN is that we will get output as NaN if the given value is not a number.

console.log(0 / 0); // Output: NaN 
console.log(parseInt("Ravi")) //Output: NaN

We have seen this everywhere: in the official documents or blogs we might have read about NaN or in videos. However, NaN does have many use cases and many misconceptions.

NaN is not equal to anything

Yes, you read it right. We cannot compare NaN to anything, not even another NaN. A simple example of this:

console.log(NaN == NaN) // Output: false

Falsy but not equal to other falsy

In Javascript, values are either truthy or falsy; NaN is considered a falsy value. Falsy values are considered false in the context of boolean values. We have seen in the above point that NaN is not even equal to NaN itself; in a similar way, NaN can’t even be compared with other falsy values.

console.log(NaN == undefined) //Output:false
console.log(NaN == false); // Outputs: false
console.log(NaN == 0); // Outputs: false
console.log(NaN == null); // Outputs: false
console.log(NaN == ""); // Output:false

This is because NaN represents an unrepresentable value, and JavaScript’s equality comparison rules treat NaN as not equal to any value, including itself.

Now the question is, how can we determine if a value is NaN or not, given that we cannot compare NaN with NaN? Well, there is a method called isNaN() for this purpose.

isNaN(NaN) //Output: true
isNaN(123) //Output: false
isNaN("abc") //Output: true
isNaN("123") //Output: false

Here, you might have observed that when we pass “123” as a string, it only considers it a number. Behind the scenes, isNaN() will first convert the given argument to a number and then check whether it’s a number or not.

However, ECMAScript 6 introduced one more function with the same name. It is associated with the number object. The major difference between these two is Number.isNaN() will return true only if the given value is NaN.

Mathematical Operations

The major use case for NaN is when we are performing mathematical operations, especially when we are not sure about the values we are going to get from APIs or dynamic values.

console.log(NaN + 5) //Output: NaN
let arr = [1, 2, NaN, 4]; 
console.log(arr.reduce((sum, num) => sum + num)); //Output: NaN

These are just some common examples of how we can use NaN and determine whether to use this function.

However, you might have observed that NaN is a very special value in Javascript. NaN is specifically used to identify if the given value is a number or not, but when it comes to comparison, you cannot even compare it with NaN itself.

Conclusion

NaN is like a special code in JavaScript that shows up when the computer tries to do a math problem that doesn’t make sense. Instead of crashing, JavaScript uses NaN to say, ‘Hey, something weird happened here.’ This helps developers notice and fix problems in their code, ensuring it keeps running smoothly even when faced with unexpected situations.

NaN acts as a helpful signal for developers, enabling them to make their code robust and capable of handling tricky situations without breaking. By recognizing and addressing the presence of NaN, developers can ensure their applications remain stable and reliable, even in the face of unexpected mathematical errors.

For more updates on the latest tools and technologies, follow the Simform Engineering Blog.

Follow us: Twitter | LinkedIn

--

--