Check if a Number Evaluates to Infinity Using JavaScript



In JavaScript, when we divide any number by zero, we can get the infinity value. Also, developers can make a mistake in writing the mathematical expression which evaluates to Infinity. So, before we perform any operation with the returned value from the mathematical expression, we need to check if the number value is finite.

Here, we will learn three approaches to check if a number evaluates to Infinity using JavaScript.

Comparing the number value with the Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY

In JavaScript, a Number is an object containing different properties and methods related to numbers. The POSITIVE_INFINITY and NEGATIVE_INFINITY properties of the Number object allow developers to evaluate the number's positive and negative infinity values.

We can compare the numerical value with the Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY to check if the number evaluates to Infinity.

Syntax

Follow the syntax below to use the POSITIVE_INFINITY and NEGATIVE_INFINITY properties of the number object.

if (num1 == Number.POSITIVE_INFINITY || num1 == Number.NEGATIVE_INFINITY) {
   
   // number is finite
} else {
   
   // number is not finite
}

In the above syntax, we have used the OR (||) operator to evaluate multiple conditions in the if statement.

Example

In this example, we have defined two numbers with different values. Num1 contains the finite values, and num2 contains the infinite value. The checkNumberIsFinite() function takes the number value as a parameter and prints the message accordingly, whether the number is finite or not, by comparing the number with the POSITIVE_INFINITY and NEGATIVE_INFINITY.

<html>
<body>
   <p>Comparing the number value with the <i> Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY </i> to check if number evaluates to Infinity.</p>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById("output");
      let num1 = 23;
      let num2 = 12 / 0;
      function checkNumberIsFinite(num) {
         
         // compare the numerical value with the Number.POSITIVE_INFINITY //and Number.NEGATIVE_INFINITY
         if (
            num == Number.POSITIVE_INFINITY ||
            num == Number.NEGATIVE_INFINITY
         ) {
            output.innerHTML += "The num is finite and it's value is " + num1 + "<br/>";
         } else {
            output.innerHTML += "The num is not finite <br/>";
         }
      }
      checkNumberIsFinite(num1);
      checkNumberIsFinite(num2);
   </script>
</body>
</html> 

Using the isFinite() method

The isFinite() method takes the numeric value as a parameter and returns the boolean values based on whether the number is finite. Here, we will invoke the isFinite() method by taking the Number object as a reference to evaluate numbers more robustly.

Syntax

Users can follow the syntax below to use the isFinite() method to check whether the number evaluates to Infinity. We have taken the Number object as a reference and passed the numeric value as a parameter.

if (Number.isFinite(num1)) {
   
   // number is finite
} else {
   
   // number evaluates to infinite
} 

Parameters

  • num1 ? It is a number to evaluate.

Return Value

  • It returns the Boolean value based on whether the number is finite or infinite.

Example

We have used the isFinite() method as a condition of the if-else statement. The isFinite() method returns the true or false based on the number value we pass as a parameter. Control of program execution goes to the if or else block according to the return value.

<html>
<body>
   <h3>Using the <i>isFinite()</i> method to check if number evaluates to Infinity.</h2> 
   <div id = "output"> </div>
   <script>
      let Output = document.getElementById("output");
      let num1 = -93;
      let num2 = -12 / 0;
      
      // using the isFinite method;
      if (Number.isFinite(num1)) {
         Output.innerHTML += "The num1 is finite and its value is " + num1 + "<br/>";
      } else {
         Output.innerHTML += "The num1 is not finite <br/>";
      }
      if (Number.isFinite(num2)) {
         Output.innerHTML += "The num2 is finite and its value is " + num2 + "<br/>";
      } else {
         Output.innerHTML += "The num2 is not finite <br/>";
      }
   </script>
</body>
</html> 

Using the Math.abs() method and Infinity keyword

The Math.abs() method allows us to get the absolute value of any number. Infinity is a keyword in JavaScript which represents the infinity value.

We can compare our number with the Infinity and -Infinity both or take the absolute value of the number and compare it with only Infinity.

Syntax

Users can use the syntax below to use the Math.abs() method and Infinity keyword to check if number evaluates to Infinity.

let number = Math.abs(num);
if (number == Infinity) {
   
   // num is not finite.
} else {
   
   // num is finite
}

Example

The example below contains the evaluateNumber() function, which invokes when the user clicks on the evaluate number button. The evaluteNumber() function first converts the number value to the positive numeric value and compares it with the Infinity keyword.

<html>
<body>
   <h3>Using the <i> Math.abs() method and Infinity keyword </i> to check if number evaluates to Infinity.</h3>
   <div id = "output"> </div><br>
   <button onclick = "evaluateNumber(23324/0)"> Evaluate number </button>
   <script>
      let output = document.getElementById("output");
      function evaluateNumber(num) {
         let number = Math.abs(num);
         if (number == Infinity) {
            output.innerHTML += "The number is not finite <br/>";
         } else {
            output.innerHTML += "The number is finite. <br/>";
         } 
      }
   </script>
</body>
</html>

The best way to check if the number evaluates to Infinity is using the isFinite() method, which takes the number as a parameter and returns the result after evaluating the number. However, users can also use other approaches, as all approaches are linear.

Updated on: 2023-03-10T16:36:52+05:30

572 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements