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

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.