In JavaScript, negative infinity is a special numeric value that represents the mathematical concept of negative infinity. It is denoted by the keyword "Number.NEGATIVE_INFINITY" or simply "-Infinity". It is a value that is smaller than any other number, including negative numbers.
Here's an example code snippet that demonstrates the use of negative infinity in JavaScript:
javascriptlet x = Number.NEGATIVE_INFINITY;
console.log(x); // Output: -Infinity
if (x < 0) {
console.log("x is negative");
} else if (x > 0) {
console.log("x is positive");
} else {
console.log("x is neither positive nor negative");
}
// Output: x is negative
In the above code, we first assign the value of negative infinity to a variable x
. We then use an if-else statement to check whether x
is positive, negative or zero. Since x
is negative infinity, the condition x < 0
evaluates to true and the code inside the first if block is executed, which logs the message "x is negative" to the console.