What is Number.NEGATIVE_INFINITY constant in JavaScript?



Number.NEGATIVE_INFINITY is a special numeric value representing a value less than Number.MIN_VALUE. This value is represented as "-Infinity". It resembles infinity in its mathematical behavior. But it is different from the mathematical infinity in many ways as listed below −

  • If the negative infinity is multiplied by NaN, the result will be NaN.

  • When a negative infinity is multiplied by a positive infinity, the result will always be a negative infinity.

  • If a negative infinity is multiplied by itself, then the result will always be positive infinity.

  • Negative infinity, divided by positive infinity or by itself will return NaN.

  • When Negative infinity is divided by a negative number except for negative infinity itself will always give positive infinity.

  • If it is divided by a positive number except for the positive infinity, it will always return negative infinity.

Syntax

Following syntax will show you how you can use the negative infinity in your code

var var_name = Number.NEGATIVE_INFINITY;

Let us understand the behavior of the negative infinity in different scenarios of the points above mentioned in detail with the help of code examples.

Algorithm

Step 1 − In the first step of the algorithm, we will add a button element in the HTML document with an onclick event associated with it.

Step 2 − In this step, we will define a JavaScript function and pass it to the onclick event as a value, which it will be called by it once the user clicks the button.

Step 3 − In the last step, we will declare a constant variable that contains the negative infinity as its value and then perform different operations with different operands on it.

Example

You can try to run the following code to learn how to work with Number.NEGATIVE_INFINITY constant −

<html>
   <body>
      <h3> Number.NEGATIVE_INFINITY in JavaScript </h3>
      <p>Click the following to see the result:</p>
      <form>
      <input type="button" value="Click Me" onclick="showValue();" />
      </form>
      <p id = "result"></p>
      <script>
         function showValue() {
            const smallNumber = (-Number.MAX_VALUE) * 2
            if (smallNumber == Number.NEGATIVE_INFINITY) {
            document.getElementById("result").innerHTML="Value of smallNumber (Smallest Number): " + smallNumber ;
            }
         }
      </script>
   </body>
</html>

Example

The below example will explain everything about the behavior of the negative infinity in different scenarios

<html>
<body>
   <h2> Number.NEGATIVE_INFINITY constant in JavaScript </h2>
   <p> Click the below button to see the results. </p>
   <button onclick = "display()"> Check here </button>
   <p id = "result"> </p>
   <script>
      var result = document.getElementById("result");
      function display() {
         const smallest = Number.NEGATIVE_INFINITY;
         const largest = Number.POSITIVE_INFINITY;
         result.innerHTML += " Negative infinity multiplied by NaN: <b> " + smallest * NaN + " </b> <br> ";
         result.innerHTML += " Negative infinity multiplied by Positive infinity: <b> " + smallest * largest + " </b> <br> " ;
         result.innerHTML += " Negative infinity is multiplied by negative infinity: <b> " + smallest * smallest + " </b> <br> " ;
         result.innerHTML += " Negative infinity is divided by negative infinity: b> " + smallest / smallest + " </b> <br> " ;
         result.innerHTML += " Negative infinity is divided by positive infinity: <b> " + smallest / largest + " </b> <br> ";
      }
   </script>
</body>
</html>

In the above example, we have performed various operations on the negative infinity with different operands on the right side to see the behaviour of the negative infinity when it is divided or multiplied with different operands that are mentioned in the points written above.

Let us consider one more code example, where we will see how we can use the negative infinity in your code to solve real time problems in your projects.

Algorithm

Step 1 − In first step, we will add an input element with number type in the HTML document to get a number input from the user.

Step 2 − In this step, we will add button element with onclick event, which takes a call back function as its value and invoke the same once the user clicks the button.

Step 3 − In the next step, we define a JavaScript function and write the logic of performing the operations and the to show the result of the operations on the user screen.

Example

The below example will illustrate the working of the negative infinity to solve the problems or bugs in your code sometimes

<!DOCTYPE html>
<html>
<body>
   <h2> Number.NEGATIVE_INFINITY constant in JavaScript </h2>
   <p> Enter a number: </p>
   <input type = "number" id = "inp1"> <br> <br>
   <button onclick = "display()"> Check here </button>
   <p id = "result"> </p>
   <script>
      var result = document.getElementById("result");
      function display() {
         var inp1 = document.getElementById("inp1");
         var num = inp1.value;
         const smallest = Number.NEGATIVE_INFINITY;
         var small = (-Number.MAX_VALUE) * num;
         if (smallest == small) {
            result.innerHTML += " The value of <b> (-Number.MAX_VALUE) * " + num + " </b> expression is similar to the value of <b> Number.NEGATIVE_INFINITY </b>. <br> ";
         } else { 
            result.innerHTML += " The value of <b> (-Number.MAX_VALUE) * " + num + " </b> expression is not same as the value of <b> Number.NEGATIVE_INFINITY </b>. <br> ";
         }
      }
   </script>
</body>
</html>

In this example, first we take a number as input from the user and then multiply the same number with the maximum positive value with negative sign in JavaScript i.e. -Number.MAX_VALUE. If the entered number is a positive number, then the product of that number with the maximum positive value with negative sign will always same as the value of the negative value independent of the value entered, it will only consider the sign such that if the sign is negative than the product value will be equal to the positive infinity instead of the negative infinity.

In this article, we have seen that what is Number.NEGATIVE_INFINITY in JavaScript and the behaviour of the same in different scenarios with help of the code examples. We have discussed two different code examples with different working to show the behaviour of the negative infinity.


Advertisements