JavaScript Infinity

`Infinity` is a JavaScript numeric value representing a magnitude beyond the finite range of the `Number` type. It can arise from operations such as dividing a nonzero number by zero and participates in ordinary numeric comparisons and arithmetic.

JavaScript represents `Infinity` as a special numeric value within the `Number` type. Both positive infinity (`Infinity`) and negative infinity (`-Infinity`) exist.

Unlike an exception, producing infinity from an ordinary numeric operation does not automatically stop execution. For example, `1 / 0` evaluates to `Infinity`, while `-1 / 0` evaluates to `-Infinity`.

You can test for infinity with `Number.isFinite()` or compare directly with `Infinity`. `Number.isFinite(value)` is generally useful when the requirement is to accept only finite numeric values because it does not coerce non-number values.

Infinity is greater than every finite JavaScript number, and negative infinity is less than every finite number. Arithmetic involving infinity follows IEEE 754 floating-point rules, so some combinations produce `Infinity`, while expressions such as `Infinity - Infinity` produce `NaN`.

Key points

Interview questions

What is `Infinity` in JavaScript?

`Infinity` is a special numeric value in JavaScript's `Number` type. It represents a magnitude beyond the finite range of the number representation, with both positive and negative forms. It can be produced by operations such as dividing a nonzero number by zero. It is still a number according to JavaScript's type system.

What does `1 / 0` return in JavaScript?

`1 / 0` evaluates to positive `Infinity`. JavaScript does not throw an exception for this ordinary numeric operation. The corresponding negative operation, such as `-1 / 0`, produces `-Infinity`.

How should you check whether a number is finite?

Use `Number.isFinite(value)` when you want to require that the value is a finite JavaScript number. It returns false for `Infinity`, `-Infinity`, and `NaN`. It also avoids the coercion performed by the global `isFinite()` function. This makes it useful for validating numeric input after ensuring the application expects a number.

Why is `Number.MAX_VALUE` different from `Infinity`?

`Number.MAX_VALUE` is the largest finite value representable by JavaScript's `Number` format. `Infinity` is a separate special numeric value used for magnitudes outside that finite range. Therefore, `Infinity > Number.MAX_VALUE` is true. Reaching a value beyond the finite range can result in infinity rather than another larger finite number.

Can arithmetic with `Infinity` produce `NaN`?

Yes. IEEE 754 floating-point arithmetic defines several invalid combinations involving infinity that result in `NaN`. A common example is `Infinity - Infinity`, which has no determinate finite or infinite result. Similarly, `Infinity * 0` produces `NaN`. This is important when calculations can reach non-finite values because subsequent arithmetic may stop producing meaningful numeric results.