Introduction
Welcome readers! The value Infinity is needed in many programming problems, and Javascript supports the invaluable Positive and Negative Infinity to help the programmers. In this blog, we will learn about them and their properties and an example code.
Following browsers support Negative and positive Infinity.
- Google Chrome
- Mozilla Firefox
- Apple Safari
- Opera
- Internet Explorer/Edge
Negative Infinity
In JavaScript, negative infinity is a constant number used to indicate the lowest possible value. This suggests that no other number is smaller than this one. It can be created with a custom function or by performing an arithmetic operation.
JavaScript shows the NEGATIVE_INFINITY value as -Infinity.
Syntax:
Number.NEGATIVE_INFINITY
Properties of Negative Infinity
Following are the properties of Negative Infinity:
- When negative infinity is divided by any other number, the result is 0.
- Negative infinity returns NaN when divided by itself or by positive infinity.
- When any positive number (other than positive infinity) is divided by negative infinity, the result is negative infinity.
- Positive infinity is negative infinity divided by any negative number (other than negative infinity).
- When negative infinity is multiplied by NaN, the outcome is NaN.
- The sum of NaN and negative infinity equals zero.
- When two negative infinities are added together, the result is always a positive infinity.
- Negative infinity is always the product of both positive and negative infinity.
Code
function checkNumber(smallNumber) {
if (smallNumber === Number.NEGATIVE_INFINITY) {
return 'Process number as -Infinity';
}
return smallNumber;
}
console.log(checkNumber(-Number.MAX_VALUE));
// expected output: -1.7976931348623157e+308
console.log(checkNumber(-Number.MAX_VALUE * 2));
// expected output: "Process number as -Infinity"
Output
You can practice by yourself with the help of Online Javascript Compiler for better understanding.