Where limits end but sequences continue - there begins infinity!
But to know exactly what we’re dealing with, let’s use the typeof operator to find out what data type we have!
typeof Infinity // "number"
Let’s look at the first case of Infinity
1 / 0 //Infinity
1 / -0 //-Infinity
Why does this happen?
It stems from the mathematical concept of limits:
$$\lim_{x \to 0^+} \frac{1}{x} = +\infty$$$$\lim_{x \to 0^-} \frac{1}{x} = -\infty$$When we divide by a number approaching zero from the positive side ($0^+$), the result grows to $+\infty$.
When we divide by a number approaching zero from the negative side ($0^-$), the result decreases to $-\infty$.
JavaScript distinguishes between +0 and -0, which is why 1/0 gives Infinity, and 1/-0 gives -Infinity.
As you can see - it didn’t take much effort to see this case in action.
Let’s try to find it then :)
let value = 1;
while (value !== Infinity) {
let next = value * 2;
if (next === Infinity) {
console.log('Last value:', value.toExponential());
console.log('Next value:', next);
break;
}
value = next;
}
Output:
Last value: 8.98846567431158e+307
Next value: Infinity
As you can see - finding infinity isn’t that hard - JavaScript makes it much easier by providing the maximum value before entering Infinity instead of tedious and inefficient iteration:
Number.MAX_VALUE
Output:
1.7976931348623157e+308
What does 1.7976931348623157e+308 before infinity (Infinity) represent?
179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000
This is an unimaginably large number, greater than the number of atoms in the universe $\sim 10^{80}$ or the number of possible chess games $\sim 10^{120}$
Why this particular value? - when the standard was defined in 1985, RAM was expensive, and choosing 64 bits for double was a deliberate design decision.
This value is sufficient for engineering calculations, most physics simulations, astronomy, and finance.
So it was a good compromise for good precision (15-17 digits), hardware efficiency, and universality (supported by every processor).
IEEE 754 Standard as the Infinity Regulator
The IEEE 754 standard defines both NaN and Infinity, so this is not just a JavaScript feature but also applies to other programming languages!
#include <iostream>
#include <limits>
int main()
{
double max = std::numeric_limits<double>::max();
double inf = std::numeric_limits<double>::infinity();
if (inf > max)
std::cout << inf << " is greater than " << max << '\n';
}
Output:
inf is greater than 1.79769e+308
Why was inf printed instead of a number?
Infinity is a value that can be represented in bits but doesn’t represent any specific numeric value - instead, it represents the concept of mathematical infinity. So if someone wanted to print the raw bits, they would get:
+Infinity = 0x7FF0000000000000
-Infinity = 0xFFF0000000000000
inf is simply more readable and convenient than bit representation, just like Infinity in JavaScript.
Why was this concept introduced in the IEEE 754 standard?
Instead of crashing the program on division by zero or overflow, IEEE 754 gives you a value you can continue working with:
const result = calculate_something();
if (result === Infinity) {
console.error("Value too large!");
return Number.MAX_VALUE;
}
Without Infinity - crash or undefined behavior.
Also, thanks to Infinity, we maintain mathematical correctness:
1 / Infinity === 0 // lim(1/x) as x→∞
Infinity + 5 === Infinity // ∞ + c = ∞
Infinity * Infinity // ∞ × ∞ = ∞
But beware - some operations with Infinity yield NaN (undefined):
Infinity - Infinity // NaN (∞ - ∞ is undefined)
Infinity * 0 // NaN (∞ × 0 is undefined)
Infinity / Infinity // NaN (∞ / ∞ is undefined)
Practical Use Cases
Disabling timeout:
const config = {
timeout: debugMode ? Infinity : 5000 // no timeout in debug
};
setTimeout(() => {
console.log('This will never execute');
}, config.timeout);
Note: Some browsers limit timeout to 32-bit int (~24 days max).
Infinitymay be converted to this value.
Sorting:
const items = [
{ name: 'Alice', priority: 1 },
{ name: 'Bob', priority: 2 },
{ name: 'System', priority: Infinity } // Always at the end
];
items.sort((a, b) => a.priority - b.priority);
Links:
