- -- operator
- -= operator
- ++ operator
- += operator
- Accessing and setting content
- Array concat() method
- Array indexOf()
- Array length
- Array pop()
- Array shift
- Arrays
- Booleans
- Braces
- Callback function
- Calling the function
- Class
- Closure
- Code block
- Comment
- Conditions
- Console
- Constructor
- Creating a p element
- Data types
- Date getTime()
- Destructuring
- Else
- Else if
- Enum
- Equals operator
- Error Handling
- ES6
- Event loop
- Events
- Extend
- Fetch API
- Filter
- For loop
- forEach()
- Function
- Function bind()
- Function name
- Greater than
- Head element
- Hoisting
- If statement
- includes()
- Infinity property
- Iterator
- JSON
- Less than
- Local storage
- Map
- Methods
- Module
- Numbers
- Object.keys()
- Overriding methods
- Parameters
- Promises
- Random
- Reduce
- Regular expressions
- Removing an element
- Replace
- Scope
- Session storage
- Sort
- Splice
- String
- String concat()
- String indexOf()
- Substring
- Switch statement
- Template literals
- Ternary operator
- Tile
- Type conversion
- While loop
JAVASCRIPT
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:
console.log(Infinity); // Infinity
You can also get this value through operations like division by zero:
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:
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:
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:
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
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:
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
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
let value = 1 / 0;
if (value === Infinity) {
console.log("Value is infinite");
}
You can also use isFinite()
to exclude infinite values:
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"
:
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:
console.log(Infinity + 1); // Infinity
console.log(Infinity - 100); // Infinity
- Multiplying by positive numbers returns Infinity:
console.log(Infinity * 2); // Infinity
- Multiplying by zero results in NaN:
console.log(Infinity * 0); // NaN
This is one of the few cases where Infinity produces an invalid number.
Use Infinity for Default Values
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:
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:
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
:
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:
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.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.