JavaScript Reference ยท Global Functions and Values
The Infinity value
Reference for JavaScript Infinity and -Infinity: how they arise from division and overflow, arithmetic rules, comparisons, and checks with Number.isFinite().
Infinity is a numeric value greater than every other number. JavaScript produces it when a result is too large to represent, or when you divide a non-zero number by zero. Its mirror image is -Infinity.
It is a regular Number value, so it takes part in arithmetic and comparisons. It is also handy as a starting point when searching for a minimum.
1 / 0; // Infinity -1 / 0; // -Infinity
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Infinity -Infinity Number.POSITIVE_INFINITY Number.NEGATIVE_INFINITY
Value: the Number value that represents positive infinity (-Infinity for negative).
Property attributes: Infinity on the global object is not writable, not enumerable and not configurable.
Usage notes
- Sources: division of a non-zero number by
0, results larger than about1.8e308(overflow),Math.pow(0, -1), andMath.max()with no arguments (which is-Infinity). - Arithmetic:
Infinity + 1is stillInfinity;Infinity - Infinity,Infinity * 0andInfinity / InfinityareNaN;x / Infinityis0for finitex. - Equality works normally, unlike
NaN:Infinity === Infinityistrue. - Testing:
Number.isFinite(x)isfalsefor infinite values;x === Infinity || x === -Infinitytests for them directly. - Strings:
String(Infinity)is"Infinity"andNumber("Infinity")parses it back.parseInt("Infinity")isNaN, butparseFloat("Infinity")isInfinity. - JSON:
JSON.stringifywritesnullfor infinite numbers. - Integer division by zero is not an error in JavaScript, unlike many languages. BigInt division by
0nthrows aRangeError. - Loops:
for (let i = 0; i < Infinity; i++)never ends unless you break out, which is useful in generators. - Not the same as a huge number:
Number.MAX_SAFE_INTEGERis only about 9 quadrillion; far larger finite numbers exist beforeInfinity.
Browser support
Infinity works in every browser and runtime.
Related
NaN,isFinite(),Number.isFinite(),Math.max()- Lessons: Numbers, Arithmetic operators