What is NaN in JavaScript? What is its type? How can you reliably test if a value is equal to NaN?

anewman15

Abdullah Al Numan

Posted on July 18, 2022

What is NaN in JavaScript? What is its type? How can you reliably test if a value is equal to NaN?

In JavaScript, NaN is a property of the global Object. In other words, it is a variable available in the Global scope.

It stands for Not-A-Number but interestingly, its type is number.

console.log(typeOf NaN); // "number"
Enter fullscreen mode Exit fullscreen mode

It is used to denote an object that is not or does not compute to a number, in a context when number operations are applied on that object.

Another interesting fact about NaN is, it never equals to itself. So NaN == NaN or NaN === NaN is always false.

console.log(NaN == NaN); // false
console.log(NaN === NaN); // false

Enter fullscreen mode Exit fullscreen mode

Testing for NaN

Since a NaN is never equal to another NaN, a self-comparison of a value makes it the most reliable way to test if the value is NaN.

function isThisNaN(value) { return value !== value };

isThisNaN(1); // false
isThisNaN(NaN); // true
isThisNaN(Number.NaN); // true
isThisNaN('NaN'); // false
Enter fullscreen mode Exit fullscreen mode

Other ways to test if an object is NaN are using the isNaN() global method and Number.isNaN().

console.log(isNaN('hi')); //true
console.log(isNaN('4'); // false
Enter fullscreen mode Exit fullscreen mode

In the two examples above, isNaN() waits for type coercion on the string before it makes the comparison. In the first case with 'hi', the string is coerced to number, which then evaluates to NaN because it doesn't return a number. In the second case with '4', it gets evaluated to a number so it is not a NaN. So using isNaN() is not very reliable to test for NaN

In contrast, Number.isNaN() tests the current value:

console.log(Number.isNaN('hi')); // false
console.log(Number.isNaN('4')); // false (this time because 
                                // it's a string in the
                                // context of a Number method)
Enter fullscreen mode Exit fullscreen mode

Type coercion is not present with Number.isNaN(). Instead, it compares the string directly. In the code above, both 'hi' and '4' are strings and therefore not NaN in the context of a Number method. This makes Number.isNaN() more reliable than isNaN() while testing for NaN values.


References

  1. NaN
  2. How can you reliably test if a value is equal to NaN?
💖 💪 🙅 🚩
anewman15
Abdullah Al Numan

Posted on July 18, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Introduction to NodeJS
node Introduction to NodeJS

October 23, 2024

Create a Node Server using Hono
javascript Create a Node Server using Hono

July 4, 2024

All About NPM (Node Package Manager)
javascript All About NPM (Node Package Manager)

May 25, 2024

What are HTTP headers in a REST API?
javascript What are HTTP headers in a REST API?

October 31, 2023