JavaScript Infinity Property: Syntax, Usage, and Examples
The Infinity
property in JavaScript represents a numeric value that is greater than any other number. It’s a special constant that behaves like a number and can be used in mathematical operations and comparisons. You can access it directly using the global identifier Infinity
. Learning how JavaScript Infinity works helps you handle edge cases, detect overflows, and manage calculations involving extremely large values.
Whether you’re writing number-heavy code or handling division results, understanding how to work with infinity JavaScript values is essential for avoiding bugs and writing robust logic.
How to Use JavaScript Infinity
The Infinity
property is a predefined global constant and doesn’t require an object or method to access it:
jsx
console.log(Infinity); // Infinity
You can also get this value through operations like division by zero:
jsx
console.log(1 / 0); // Infinity
JavaScript evaluates such expressions and automatically assigns them the value of Infinity
.
When to Use Infinity JavaScript
Detect Division by Zero
JavaScript doesn’t throw an error when dividing by zero. Instead, it returns Infinity or -Infinity:
jsx
console.log(10 / 0); // Infinity
console.log(-10 / 0); // -Infinity
Use this behavior to identify when computations might be invalid or need special handling.
Represent Unbounded Limits
In simulations or algorithms, you might need to define a maximum boundary:
jsx
let minDistance = Infinity;
for (let distance of [100, 50, 75]) {
if (distance < minDistance) {
minDistance = distance;
}
}
console.log(minDistance); // 50
This pattern works well when searching for the minimum value in a list.
Compare Against Very Large Numbers
You can check if a value is exceedingly large:
jsx
let huge = Number.MAX_VALUE * 2;
console.log(huge === Infinity); // true
This can help prevent number overflow in large-scale calculations.
Examples of JavaScript Infinity in Practice
Basic Comparison
jsx
console.log(Infinity > 1000000); // true
console.log(Infinity === Infinity); // true
Infinity always evaluates as greater than any finite number and is equal to itself.
Negative Infinity
JavaScript also has a -Infinity
value:
jsx
console.log(-Infinity < -999999); // true
console.log(-Infinity === -Infinity); // true
It behaves like a number smaller than all other numbers.
Using Infinity in Loops or Bounds
jsx
let bestScore = -Infinity;
const scores = [70, 85, 92];
for (let score of scores) {
if (score > bestScore) {
bestScore = score;
}
}
console.log(bestScore); // 92
Start comparisons with -Infinity
when looking for the maximum value.
Check If a Value Is Infinity
jsx
let value = 1 / 0;
if (value === Infinity) {
console.log("Value is infinite");
}
You can also use isFinite()
to exclude infinite values:
jsx
console.log(isFinite(1 / 0)); // false
console.log(isFinite(42)); // true
Learn More About JavaScript Infinity
Infinity Is a Number
Even though it behaves unusually, typeof Infinity
returns "number"
:
jsx
console.log(typeof Infinity); // "number"
So Infinity participates in numeric operations just like regular numbers.
Infinity in Arithmetic Operations
- Adding or subtracting Infinity with a number still yields Infinity:
jsx
console.log(Infinity + 1); // Infinity
console.log(Infinity - 100); // Infinity
- Multiplying by positive numbers returns Infinity:
jsx
console.log(Infinity * 2); // Infinity
- Multiplying by zero results in NaN:
jsx
console.log(Infinity * 0); // NaN
This is one of the few cases where Infinity produces an invalid number.
Use Infinity for Default Values
jsx
let lowest = Infinity;
let highest = -Infinity;
These defaults work well in search and comparison logic.
Infinity in Arrays
When working with arrays of numbers, Infinity can simplify initialization:
jsx
let times = [400, 300, 500, 200];
let fastest = Infinity;
for (let t of times) {
if (t < fastest) fastest = t;
}
console.log(fastest); // 200
This is more readable and reliable than starting with a high constant like 999999
.
Overflow and Precision
JavaScript doesn’t have built-in overflow detection, but if a number exceeds Number.MAX_VALUE
, it automatically becomes Infinity:
jsx
let big = Number.MAX_VALUE * 2;
console.log(big); // Infinity
This can signal that your calculation exceeded the range of safe floating-point values.
Infinity with JSON
When you try to convert Infinity to JSON, it becomes null
:
jsx
const obj = { value: Infinity };
console.log(JSON.stringify(obj)); // {"value":null}
If you’re serializing data, be aware that infinite values won’t translate properly.
In Comparisons
Infinity works with comparison operators:
jsx
console.log(Infinity > 1000000000000); // true
console.log(Infinity === Infinity); // true
console.log(Infinity < Infinity); // false
But remember that no number, however large, will ever equal Infinity.