Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
1 / 0;          // Infinity
-1 / 0;         // -Infinity

Try it

JS Producing and using Infinity

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS A starting value and checks

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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 about 1.8e308 (overflow), Math.pow(0, -1), and Math.max() with no arguments (which is -Infinity).
  • Arithmetic: Infinity + 1 is still Infinity; Infinity - Infinity, Infinity * 0 and Infinity / Infinity are NaN; x / Infinity is 0 for finite x.
  • Equality works normally, unlike NaN: Infinity === Infinity is true.
  • Testing: Number.isFinite(x) is false for infinite values; x === Infinity || x === -Infinity tests for them directly.
  • Strings: String(Infinity) is "Infinity" and Number("Infinity") parses it back. parseInt("Infinity") is NaN, but parseFloat("Infinity") is Infinity.
  • JSON: JSON.stringify writes null for infinite numbers.
  • Integer division by zero is not an error in JavaScript, unlike many languages. BigInt division by 0n throws a RangeError.
  • 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_INTEGER is only about 9 quadrillion; far larger finite numbers exist before Infinity.

Browser support

Infinity works in every browser and runtime.