← Back to list

JS — Interview Question: isNaN() and Number.isNaN()

Let’s begin our interview question series by learning the difference between isNaN() and Number.isNaN(), as this question is frequently…

Javid Salmanov · 2024-10-19 11:12 · 6 claps · 2.9 min read paywalled
#javascript #isnan #not-a-number #interview #programming
Open on Medium ↗
Wiki topics: STP · Startups & Venture EDU · Education & Learning 💻 · Programming 🌐 · Web Development

JS — Interview Question: isNaN() and Number.isNaN()

Let’s begin our interview question series by learning the difference between isNaN() and Number.isNaN(), as this question is frequently asked. If you’re working on projects with a heavy focus on mathematics, such as an app for coin trading, you’re likely to encounter this question quite often. So, what’s the difference between these two? Aren’t they the same thing?

NaN

NaN is an abbreviation for the English phrase “Not A Number

In JavaScript, you can encounter NaN in these 5 situations:

  1. Failed number conversion

NaN is returned when a number conversion fails.

  1. Mathematical operation that isn’t a real number

NaN is also returned in cases where the result is not a real number:

  1. Indeterminate form

In mathematics, NaN is returned in cases of indeterminate forms:

  1. Operations interacting with NaN

If one of the operands is NaN or is forced to become NaN, the result will be NaN. This shows that NaN is “contagious”:

  1. Other situations where an invalid value must be represented as a number

NaN is returned when an invalid value must be represented as a number:

If you wanted to test whether an output in your project is NaN, how would you do it? The first thought that comes to mind is probably to write code like the one below, right? :)

As you can see, if we do something like this, JavaScript will always return false. That’s why we need to look for alternative methods. This is exactly where isNaN() and Number.isNaN() come into play.

isNaN()

isNaN() is a built-in JavaScript function that performs a forced conversion (coercion) if necessary on the value it takes as a parameter. In other words, if the given value is not of the number type, it converts it to number and then checks whether it is NaN. For example, if we pass isNaN(“1”), isNaN() will first force this value to be converted to number and then test whether it is NaN. You can understand this better through the following examples:

Number.isNaN()

Number.isNaN() only tests whether the value is actually NaN. Unlike isNaN(), it does not perform any conversion of the value to the number type. If the value is NaN, it returns true; otherwise, you’ll get false:

Conclusion

In conclusion, understanding the difference between isNaN() and Number.isNaN() is crucial for selecting the correct validation method in your projects. While isNaN() automatically converts the value to the number type, Number.isNaN() only tests for actual NaN values. Therefore, choosing the right function based on your project’s needs will help prevent potential errors.


메타데이터
post_id
540423ecf07f
slug
js-interview-question-isnan-and-number-isnan-540423ecf07f
url
https://medium.com/@salmanov.cavid1/js-interview-question-isnan-and-number-isnan-540423ecf07f
canonical_url
https://medium.com/@salmanov.cavid1/js-interview-question-isnan-and-number-isnan-540423ecf07f
author_url
https://medium.com/@salmanov.cavid1
status
ok
fetched_at
2026-06-29 01:02:39