Special Values: Infinity and -Infinity
You'll learn how JavaScript uses Infinity and -Infinity to represent numbers that overflow the limits of what the Number type can hold.
What Are Infinity and -Infinity?
In JavaScript, Infinity and -Infinity are special numeric values that represent numbers too large (or too small) to fit within the normal range. Remember from earlier lessons that Number.MAX_VALUE is the largest representable number. If a calculation exceeds this limit, JavaScript doesn't crash—it gives you Infinity instead.
Think of it like a thermometer that only goes up to 120°F. If the temperature tries to go higher, the display just shows "MAX" instead of breaking. That's how JavaScript handles overflow.
How You Get These Values
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
console.log(Number.MAX_VALUE * 2); // Infinity
You can also use them directly as values:
console.log(Infinity); // Infinity
console.log(-Infinity); // -Infinity
console.log(typeof Infinity); // "number"
Behavior in Operations
These special values behave predictably in math:
console.log(Infinity + 1); // Infinity
console.log(Infinity - 1000); // Infinity
console.log(Infinity * 2); // Infinity
console.log(Infinity / 2); // Infinity
console.log(-Infinity * -1); // Infinity
console.log(Infinity + Infinity); // Infinity
console.log(Infinity - Infinity); // NaN (undefined)
Checking for Infinity
console.log(Number.isFinite(Infinity)); // false
console.log(Number.isFinite(100)); // true
Key Takeaway: Infinity and -Infinity are special Number values that JavaScript uses to represent overflow instead of throwing errors—they behave like mathematical infinity in operations.