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

Special Value: NaN (Not a Number)

Understand that NaN results from invalid numeric operations and is the only value not equal to itself.

Special Value: NaN (Not a Number)

You'll learn what NaN represents, how it's created, and why it has unique comparison behavior.

What is NaN?

NaN stands for "Not a Number," but ironically, its type is Number. It's JavaScript's way of representing the result of an invalid or undefined numeric operation. Think of it as the number system's error flag—when you try to do math that doesn't make sense, you get NaN instead of crashing.

How NaN Gets Created

You'll encounter NaN when numeric operations fail:

let result1 = 0 / 0;           // NaN (undefined math)
let result2 = Math.sqrt(-1);   // NaN (no real square root)
let result3 = parseInt("hello"); // NaN (can't parse text as number)
let result4 = Number(undefined); // NaN

The Quirky Truth: NaN ≠ NaN

Here's the most important thing about NaN: it's the only value in JavaScript that is not equal to itself:

NaN === NaN;  // false (!!)
NaN == NaN;   // false

This seems bizarre, but it's intentional. Since NaN represents "something went wrong with math," and different operations can fail in different ways, JavaScript treats each NaN as unique. It's like saying "I don't know" ≠ "I don't know"—they're both unknowns, but not necessarily the same unknown.

Checking for NaN

Because direct comparison fails, you must use Number.isNaN():

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

Key Takeaway: NaN results from invalid numeric operations and is the only JavaScript value that doesn't equal itself—always use Number.isNaN() to detect it.