Course contentsShow
← JavaScript
Lesson 242 of 2,7237. Arithmetic and OperatorsFree lesson

NaN in Arithmetic Operations

Learn how invalid arithmetic like 0/0 or 'text' * 2 produces NaN, and how NaN propagates through calculations.

NaN in Arithmetic Operations

What you'll learn: How JavaScript produces NaN when arithmetic goes wrong, and why it spreads through your calculations like a warning signal.

What is NaN?

NaN stands for "Not-a-Number," but ironically, it is a numeric value in JavaScript—just a special one that represents an invalid or undefined mathematical result. Think of it as JavaScript's way of saying "I tried to do math, but this doesn't make sense."

Operations That Produce NaN

Certain arithmetic operations cannot produce a meaningful number:

0 / 0              // NaN (undefined in mathematics)
'hello' * 2        // NaN (can't multiply text)
'text' - 10        // NaN (can't subtract from non-numeric strings)
5 * undefined      // NaN (undefined isn't a number)

Even though 5 / 0 gives you Infinity (as you learned previously), dividing zero by itself is mathematically undefined, so JavaScript returns NaN.

NaN is Contagious

Once NaN appears in a calculation, it propagates through all subsequent operations:

let result = 'abc' * 3;  // NaN
result + 10;             // NaN
result - 5;              // NaN
result * 2;              // NaN

This behavior is actually helpful—it ensures you don't accidentally get a "normal-looking" number from corrupted calculations. The NaN acts like a red flag that travels with your data.

Why This Matters

When you see NaN in your results, it's JavaScript telling you that somewhere along the line, invalid data entered your arithmetic. It's a debugging clue pointing to bad input or a logic error.

Key Takeaway: NaN results from invalid arithmetic operations (like 0/0 or math with non-numeric values), and once it appears, it contaminates all further calculations—serving as a warning that something went wrong.