The JavaScript Infinity Property

Infinity is a mathematical concept that refers to a quantity without a bound.

The JavaScript Infinity property is a global property representing infinity. The initial value of Infinity is Number.POSITIVE_INFINITY. Infinity is greater than any other number, including itself.

Infinity usually occurs when the result of an operation is impossible to represent because it is too large or too small to fit into a float. As a value, Infinity indicates division by zero or an arithmetic overflow or underflow, for example when the exponent of a floating point number is too large:

Math.pow(2, 1024); // Infinity

Although arithmetically undefined, division by zero in JavaScript is valid and has well-defined results1:

  • 1 / 0 yields Infinity
  • (-1) / 0 yields -Infinity
  • 0 / 0 yields NaN

As a result, JavaScript will not throw an error when dividing by zero so it is up to the programmer to check whether the divisor of a fraction is not 0.

The isFinite function can be used to check if a given value is a finite number.

See also

  1. ^ JavaScript uses the IEEE 754 standard to perform all of its arithmetic operations

If you see a typo, want to make a suggestion or have anything in particular you'd like to know more about, please drop us an e-mail at hello at diveintojavascript dot com.

Copyright © 2010-2013 Dive Into JavaScript