Course contentsShow
← JavaScript
Lesson 36 of 2,7232. Values and TypesFree lesson

Special Number Values: NaN, Infinity

Learn about NaN (Not-a-Number) and Infinity as special members of the number type.

Special Number Values: NaN, Infinity

What you'll learn: JavaScript's number type includes two unusual values—NaN and Infinity—that represent special mathematical situations.

What Are These Special Values?

While most numbers behave predictably (like 42 or 3.14), JavaScript's number type has two special members that represent edge cases:

NaN stands for "Not-a-Number"—ironically, it is a number type! Think of it as JavaScript's way of saying "this calculation doesn't make mathematical sense." You'll encounter NaN when you attempt impossible math operations.

Infinity represents a value larger than any other number—essentially, mathematical infinity. Its counterpart, -Infinity, represents negative infinity.

When Do These Appear?

NaN shows up when math goes wrong:

  • Trying to do arithmetic with something that isn't a valid number
  • Operations like dividing zero by zero
  • Any calculation that produces an undefined result

Infinity appears when numbers get too large:

  • Dividing a positive number by zero gives Infinity
  • Dividing a negative number by zero gives -Infinity
  • Numbers that exceed JavaScript's maximum value

The Quirky Nature of NaN

Here's something strange: NaN is the only value in JavaScript that doesn't equal itself! This makes sense if you think about it—if two calculations both fail, they didn't fail in the same way, so they're not "equal."

Both NaN and Infinity are actual number values. If you check their type, JavaScript confirms they're numbers, even though they represent special cases.

Key Takeaway: NaN signals invalid math operations and never equals itself, while Infinity represents values beyond JavaScript's numerical limits—both are legitimate members of the number type.