Infinity
Represents positive infinity, the value arithmetic overflow and division by zero produce.
Infinity- Added in JavaScript ECMAScript 1 (1997)
Return value
numberThe numeric value representing positive infinity — the same value as Number.POSITIVE_INFINITY.
Changelog
- PHP ECMAScript 5Made non-writable, non-enumerable and non-configurable. In ES3 the global binding could be reassigned.
Infinity is a real value of type number, not an error. JavaScript's arithmetic never throws on overflow or on division by zero — it saturates to Infinity or -Infinity and keeps computing, which is why a bad value can travel a long way before anyone notices.
Where it comes from
1 / 0, and any division of a non-zero number by zero.- Overflow past
Number.MAX_VALUE, as in1e308 * 10. Math.max()with no arguments returns-Infinity;Math.min()returnsInfinity.parseFloat("Infinity")andNumber("Infinity").
Where it is useful
As a sentinel for comparison loops: seed a running minimum with Infinity and a running maximum with -Infinity, and the first real value always wins. It is also a natural "no limit" value for a numeric option.
What trips people up
JSON.stringify() serialises both Infinity and NaN as null, silently, so an overflow in a computed field becomes a null in your API response. Infinity - Infinity and Infinity / Infinity are NaN, not zero or one. And 1 / -Infinity is -0, which compares equal to 0 but is distinguishable with Object.is().
Example
When to use it
- Seed a running minimum with Infinity so the first compared value always replaces it.
- Express "no maximum" for a limit, timeout or page-size option.
- Detect overflow in a computation before the value is stored or serialised.
Frequently asked questions
What does Infinity mean in JavaScript?
It is a numeric value larger than any finite number, produced by division by zero and by arithmetic overflow past Number.MAX_VALUE. It is a normal value of type number, so operations on it succeed rather than throwing.
Why does dividing by zero return Infinity instead of throwing?
Because JavaScript numbers follow IEEE 754, which defines n / 0 as an infinity and 0 / 0 as NaN rather than as errors. Guard the result with Number.isFinite if the calling code cannot handle it.
How do I check whether a value is Infinity?
Compare directly with Infinity or -Infinity, or use Number.isFinite(x) to rule out both infinities and NaN at once. Note that isFinite(null) is true because of coercion, so prefer the Number.isFinite form.
More examples
Infinity as a comparison seed
Any real number beats the seeds on the first iteration, so no special-casing of the first element is needed. The empty-array case returns the seeds unchanged, which is a clear signal that nothing was compared.
function range(values) {
let min = Infinity;
let max = -Infinity;
for (const v of values) {
if (v < min) min = v;
if (v > max) max = v;
}
return { min, max };
}
console.log(range([4, -2, 9, 0])); // { min: -2, max: 9 }
console.log(range([])); // { min: Infinity, max: -Infinity }Arithmetic that produces NaN instead
The indeterminate forms of real analysis map to NaN, while the determinate ones stay Infinity. That is the boundary where a runaway value stops being large and starts being NaN, which then poisons every subsequent comparison.
console.log(Infinity + 1); // Infinity
console.log(Infinity * 0); // NaN
console.log(Infinity - Infinity); // NaN
console.log(Infinity / Infinity); // NaN
console.log(Infinity > Number.MAX_VALUE); // true
console.log(Math.pow(0, -1)); // Infinity
console.log(Math.log(0)); // -Infinity
console.log(0 / 0); // NaNSurviving serialisation
The default output loses the distinction between an overflow and a genuine null. A replacer/reviver pair round-trips the value, though checking with Number.isFinite before serialising is usually the better fix.
const stats = { avg: 10 / 0, count: 0 };
console.log(JSON.stringify(stats)); // {"avg":null,"count":0}
// Preserve it explicitly with a replacer
const json = JSON.stringify(stats, (k, v) =>
v === Infinity ? "Infinity" : v === -Infinity ? "-Infinity" : v);
console.log(json);
console.log(JSON.parse(json, (k, v) => (v === "Infinity" ? Infinity : v)).avg);
Discussion