What is negative Infinity?

613
february 22, 2023, at 23:22

In JavaScript, Infinity and -Infinity are special values that represent positive infinity and negative infinity, respectively. These values are used to represent numbers that are too large (or too small) to be represented as regular numbers.

Negative infinity (-Infinity) is a value that is less than any other number, including negative numbers. It is the result of dividing a negative number by zero, or subtracting Infinity from a negative number. For example:

1
2
console.log(-1 / 0); // outputs -Infinity
console.log(-100 - Infinity); // outputs -Infinity
Negative infinity can also be compared to other numbers using the usual comparison operators (<, >, <=, >=). For example:

1
2
3
console.log(-10 < -Infinity); // outputs true
console.log(-Infinity < 0); // outputs true
console.log(-Infinity > Infinity); // outputs false
Note that the special values Infinity and -Infinity are not actually numbers, but are rather of the type number. They can be used in mathematical operations like regular numbers, but they have some special properties and behaviors, such as being immutable and not being equal to any other value, including themselves (i.e., Infinity !== Infinity and -Infinity !== -Infinity).