DEV Community

Abdullah Al Numan
Abdullah Al Numan

Posted on

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?

Top comments (1)

Collapse
 
anewman15 profile image
Abdullah Al Numan

Thanks!

I never used Object.is(), but this is essentially the same thing as Number.isNaN(). I'm a bit skeptical about both Number.isNaN() and Object.is() because they return true if we compare two diiferent NaN s, whereas comparing two NaN s with equality operators return false.

console.log(Object.is(NaN, NaN)); // true
console.log(Number.isNaN(NaN)); // true
console.log(NaN === NaN); // false

Enter fullscreen mode Exit fullscreen mode

I think basing the comparison on NaN never being equal to another NaN makes it interesting.

What's your take?