🚀 Introduction
In JavaScript, numbers can go beyond normal ranges. When calculations go too low or invalid operations happen, JavaScript uses special values like Infinity
and -Infinity
(Negative Infinity) to represent the result. These values are part of JavaScript’s Number type.
📚 What is Negative Infinity?
Negative Infinity (-Infinity) is a special numeric value in JavaScript. It represents a number that is smaller than all other numbers. It is not the same as NaN
(Not-a-Number). Unlike NaN
, Negative Infinity is still treated as a number.
Example:
console.log(-Infinity); // -Infinity
console.log(typeof -Infinity); // "number"
Here, -Infinity
is recognized as a number type in JavaScript.
🔎 How is Negative Infinity Produced?
Negative Infinity usually appears in situations where a calculation goes beyond the smallest possible number or involves invalid mathematical operations.
Examples:
console.log(-1 / 0); // -Infinity (negative divided by zero)
console.log(Math.log(0)); // -Infinity (logarithm of 0)
console.log(Number.NEGATIVE_INFINITY); // -Infinity (built-in constant)
Dividing a negative number by zero gives -Infinity
.
Taking the logarithm of zero also results in -Infinity
.
JavaScript provides a constant Number.NEGATIVE_INFINITY
to represent it.
đź§© Properties of Negative Infinity
Negative Infinity has some special behaviors in JavaScript:
Comparison:
console.log(-Infinity < 0); // true
console.log(-Infinity < -1000); // true
Negative Infinity is always smaller than any other number.
Equality:
console.log(-Infinity === Number.NEGATIVE_INFINITY); // true
Both are the same value.
Arithmetic Operations:
console.log(-Infinity + 100); // -Infinity
console.log(-Infinity * 2); // -Infinity
console.log(-Infinity * -1); // Infinity
⚠️ Negative Infinity vs NaN
It is important to understand the difference between Negative Infinity and NaN:
Negative Infinity means the value is too small to represent, but it is still a number.
NaN means the value is not a number at all.
Example:
console.log(typeof -Infinity); // "number"
console.log(typeof NaN); // "number"
console.log(isNaN(-Infinity)); // false
console.log(isNaN(NaN)); // true
Here, -Infinity
is a valid number, but NaN
is not.
🛠️ When is Negative Infinity Useful?
You may encounter Negative Infinity in these cases:
Error checking: To detect invalid divisions or logarithmic operations.
Limits: To represent the smallest possible number in calculations.
Algorithms: Some algorithms use -Infinity
as an initial minimum value when searching for the largest number.
Example (Finding Maximum):
let numbers = [3, 7, 1, 9];
let max = -Infinity;
for (let num of numbers) {
if (num > max) {
max = num;
}
}
console.log(max); // 9
Here, we start with -Infinity
so that any real number will be greater than it.
📝 Summary
Negative Infinity in JavaScript is a special numeric value that represents a number smaller than all other numbers. It often appears in situations like dividing a negative number by zero or taking the logarithm of zero. Unlike NaN, it is still considered a number and can be used in comparisons and calculations. It is especially useful in error handling and algorithm design where extreme values are needed.