Course contentsShow
← JavaScript
Lesson 122 of 2,7234. Working with NumbersFree lesson

NaN: Not a Number

Understand what NaN represents, how it arises, and why NaN !== NaN.

NaN: Not a Number

You'll learn what NaN really means, how JavaScript creates it, and why it has one very strange behavior.


What Is NaN?

NaN stands for "Not a Number," but ironically, its type is Number. It represents the result of an invalid or undefined mathematical operation. Think of it as JavaScript's way of saying "I tried to do math, but something went wrong."

How NaN Arises

You get NaN when you attempt operations that don't produce meaningful numeric results:

0 / 0                    // NaN
Infinity - Infinity      // NaN
Math.sqrt(-1)           // NaN (no real square root)
parseInt("hello")       // NaN (can't parse text as number)

The Bizarre Truth: NaN !== NaN

Here's the strangest thing about NaN: it is not equal to itself. This is unique in JavaScript:

NaN === NaN             // false
NaN == NaN              // false

Why? Because mathematically, two undefined results aren't necessarily the same undefined result. The square root of -1 and 0/0 both produce NaN, but they're conceptually different failures.

Checking for NaN

Since you can't use ===, use Number.isNaN():

Number.isNaN(NaN)       // true
Number.isNaN(0 / 0)     // true
Number.isNaN(123)       // false

Key Takeaway: NaN means "invalid number result," has type Number, and uniquely fails equality checks with itself—always use Number.isNaN() to detect it.