Infinity - Where Does JavaScript End?
Source: Dev.to
What type is Infinity?
typeof Infinity // "number"
The first case of Infinity
1 / 0 // Infinity
1 / -0 // -Infinity
Why does this happen?
It stems from the mathematical concept of limits:
[ \lim_{x \to 0^+} \frac{1}{x} = +\infty \qquad \lim_{x \to 0^-} \frac{1}{x} = -\infty ]
When we divide by a number that approaches zero from the positive side, the result grows without bound (+∞).
When we divide by a number that approaches zero from the negative side, the result decreases without bound (-∞).
JavaScript distinguishes between +0 and -0, which is why 1/0 yields Infinity and 1/-0 yields -Infinity.
Finding the largest finite number before Infinity
let value = 1;
while (value !== Infinity) {
const next = value * 2;
if (next === Infinity) {
console.log('Last value:', value.toExponential());
console.log('Next value:', next);
break;
}
value = next;
}
Output
Last value: 8.98846567431158e+307
Next value: Infinity
You don’t need to iterate to discover this limit—JavaScript provides it directly:
Number.MAX_VALUE
Output
1.7976931348623157e+308
What does 1.7976931348623157e+308 represent?
179,769,313,486,231,570,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000
An unimaginably large number—far larger than the estimated number of atoms in the universe (≈ 10⁸⁰) or the number of possible chess games (≈ 10¹²⁰).
Why this particular value?
When the IEEE‑754 double‑precision format was standardized in 1985, 64‑bit floating‑point numbers were chosen as a compromise between precision (15‑17 decimal digits), hardware efficiency, and universality. The resulting maximum finite value is Number.MAX_VALUE.
IEEE‑754 Standard – The Infinity Regulator
The IEEE‑754 standard defines both NaN and Infinity. This isn’t a JavaScript‑only feature; it appears in many languages.
#include <iostream>
#include <limits>
int main() {
double max = std::numeric_limits<double>::max();
double inf = std::numeric_limits<double>::infinity();
if (inf > max)
std::cout << "Infinity is greater than the maximum finite value\n";
return 0;
}
Note: Some browsers impose an upper bound on the timeout value, so
Infinitymay be clamped to that limit.Infinitycan be coerced to a 32‑bit integer (≈ 24 days max). In such cases it may be converted to that maximum integer value.
Summary
Infinityis a number in JavaScript (typeof Infinity === "number").- It originates from the IEEE‑754 floating‑point standard, which defines special bit patterns for
+∞and‑∞. Number.MAX_VALUE(≈ 1.7976931348623157e+308) is the largest finite double‑precision value; any larger result becomesInfinity.- Using
Infinitylets programs handle overflow and division‑by‑zero gracefully while preserving mathematical semantics.
Understanding Infinity helps you write more robust numerical code and appreciate the design decisions behind modern floating‑point arithmetic.
Sorting
const items = [
{ name: 'Alice', priority: 1 },
{ name: 'Bob', priority: 2 },
{ name: 'System', priority: Infinity } // Always at the end
];
items.sort((a, b) => a.priority - b.priority);
Enter fullscreen mode
Exit fullscreen mode