Skip to content
Related Articles

Related Articles

Improve Article

How to check if a number evaluates to Infinity using JavaScript ?

  • Last Updated : 18 Oct, 2019

The task is to check whether number evaluates to infinity or not with the help of JavaScript. Here are few techniques discussed.
Approach 1:

Example 1: This example uses the approach as discussed above.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        Check if a Number evaluates to Infinity.
    </title>
</head>
  
<body style="text-align:center;"
      id="body">
    <h1 style="color:green;" 
        id="h1">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP"
       style="font-size: 15px; 
              font-weight: bold;">
    </p>
    <button onclick="GFG_Fun()">
        click here
    </button>
    <p id="GFG_DOWN" 
       style="color:green; 
              font-size: 20px; 
              font-weight: bold;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        var n = 1 / 0;
        up.innerHTML = "Click on the button to check if"+
          " Number evaluates to Infinity.<br> Number = 1/0";
  
        function GFG_Fun() {
            if (n == Number.POSITIVE_INFINITY || n == Number.NEGATIVE_INFINITY) {
                down.innerHTML = "Infinity";
            } else {
                down.innerHTML = "Not Infinity";
            }
        }
    </script>
</body>
  
</html>

Output:

  • Before clicking on the button:
  • After clicking on the button:

Approach 2:

  • Using Number.isFinite() method to check if the number is finite or not.

Example 2: This example uses the approach as discussed above.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        Check if a Number evaluates to Infinity.
    </title>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" 
       style="font-size: 15px;
              font-weight: bold;">
    </p>
    <button onclick="GFG_Fun()">
        click here
    </button>
    <p id="GFG_DOWN" 
       style="color:green; 
              font-size: 20px; 
              font-weight: bold;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        var n = 1 / 4;
        up.innerHTML = "Click on the button to check if"+
          " Number evaluates to Infinity.<br> Number = 1/4";
  
        function GFG_Fun() {
            if (!Number.isFinite(n)) {
                down.innerHTML = "Infinity";
            } else {
                down.innerHTML = "Not Infinity";
            }
        }
    </script>
</body>
  
</html>

Output:

  • Before clicking on the button:
  • After clicking on the button:



My Personal Notes arrow_drop_up
Recommended Articles
Page :