6 min read
|
Saved February 14, 2026
|
Copied!
Do you care about this?
This article explains what NaN (not-a-number) is in JavaScript and how it behaves in mathematical operations and comparisons. It highlights the unique properties of NaN, including that it is not equal to itself, and discusses methods to check for NaN values correctly.
If you do, here's more
NaN, which stands for "Not a Number," behaves in unique ways in JavaScript and programming in general. When included in any arithmetic operation, the result is always NaN. For example, expressions like `2 + NaN` or `NaN - 50` yield NaN. If NaN appears in any calculation, it propagates through the entire expression, ensuring that the final result is still NaN. Comparisons involving NaN always return false, as NaN is neither greater than, less than, nor equal to any value, including itself. This creates a paradox: NaN is the only value in JavaScript that is not equal to itself.
The article explains how NaN functions as an error state in calculations. Its primary role is to signal that something went wrong without producing unpredictable results. NaN is classified as a number according to JavaScript standards, yet its behavior complicates detection in code. Simply checking for equality against NaN fails, as `NaN === NaN` returns false. Instead, developers can use methods like `typeof` to check if a value is a number or the global `isNaN()` function to see if a value can be coerced into a number. The newer `Number.isNaN()` method checks specifically for the value NaN without coercion.
Further, the article highlights the differences between `isNaN()` and `Number.isNaN()`. The former attempts to coerce values into numbers, while the latter strictly checks for the NaN value itself. This distinction is crucial for developers who need to handle numeric values and errors accurately. By using these methods appropriately, programmers can manage cases where calculations result in NaN, ensuring their code behaves as expected without causing confusion.
Questions about this article
No questions yet.