Number.POSITIVE_INFINITY

Holds positive infinity, the value arithmetic collapses to when it overflows the finite range.

Signature
Number.POSITIVE_INFINITY
  • Added in JavaScript ES1

Return value

numberPositive infinity — the same value as the global Infinity property. The property is non-writable, non-enumerable and non-configurable.

Errors

Nothing on read; assigning to it throws a TypeError in strict mode, because the property is non-writable.

This is the IEEE 754 positive infinity, identical to the global Infinity. It is produced by dividing a positive number by zero, by any arithmetic that overflows past Number.MAX_VALUE, and by Number("Infinity"). Its arithmetic is mostly intuitive — bigger than every finite number, absorbing under addition and multiplication — with the exceptions listed below.

When to reach for it

The most common honest use is as the seed for a minimum search: start at Number.POSITIVE_INFINITY and every real candidate wins the first comparison, with no special case for the empty list. It is also a natural sentinel for "no limit" in configuration — a page size or timeout of Infinity compares correctly against everything without needing a null check.

What trips people up

Some operations on infinity produce NaN rather than another infinity: Infinity - Infinity, Infinity / Infinity and 0 * Infinity are all indeterminate. That is how an overflow that stayed quiet turns into a NaN several steps later.

It also does not survive JSON. JSON.stringify() emits null for it, so an "unlimited" sentinel written to storage or sent over a network comes back as null and any comparison against it silently changes meaning.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Seeding a running minimum so the first real value always replaces it.
  • Expressing "no limit" for a cap, timeout, or page size that is still compared numerically.
  • Detecting overflow or division by zero in a computation pipeline.

Frequently asked questions

What is the difference between Infinity and Number.POSITIVE_INFINITY?

There is none — they are the same value, and === reports true. Number.POSITIVE_INFINITY is a static property that cannot be shadowed by a local variable named Infinity, which is why some style guides prefer it.

Why does Infinity - Infinity give NaN?

Because the result is mathematically indeterminate: there is no single answer for the difference of two unbounded quantities. Infinity / Infinity and 0 * Infinity are NaN for the same reason, while Infinity + Infinity is still Infinity.

How do I check whether a number is Infinity?

Use Number.isFinite(x), which is false for Infinity, -Infinity and NaN. For positive infinity alone, compare with x === Number.POSITIVE_INFINITY — unlike NaN, Infinity does compare equal to itself.

More examples

Where Infinity comes from

Five different routes all arrive at the same value: overflow, division by zero, string parsing and a runaway exponential. Note that Math.exp(709) is still finite at about 8.2e307 — the cliff is that close. isFinite() is the reliable test for having fallen off it.

Example · javascript
console.log(Number.MAX_VALUE * 2);   // Infinity
console.log(1 / 0);                  // Infinity
console.log(Number("Infinity"));     // Infinity
console.log(parseFloat("1e999"));    // Infinity
console.log(Math.exp(710));          // Infinity

console.log(Number.isFinite(Number.POSITIVE_INFINITY));        // false
console.log(Number.POSITIVE_INFINITY > Number.MAX_VALUE);      // true

Math.min() with no arguments

Math.min() returns Infinity when given nothing, because Infinity is the identity for minimum. Spreading an empty array therefore yields Infinity rather than an error or undefined — check Number.isFinite() on the result before treating it as a real measurement.

Example · javascript
console.log(Math.min());        // Infinity
console.log(Math.min(...[]));   // Infinity
console.log(Math.min(3, 1, 2)); // 1

const empty = [];
const smallest = Math.min(...empty);
console.log(smallest, Number.isFinite(smallest)); // Infinity false

Discussion

  • Be the first to comment on this lesson.