Saturday, October 25, 2025
HomeLanguagesJavascriptHow to check if a number evaluates to Infinity using JavaScript ?

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

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

Approach 1: Checking if the number is equal to the Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY .

Example: This example uses the approach discussed above. 

html




<body style="text-align:center;">
      id="body">
    <h1 style="color:green;" 
        id="h1">
        GeeksForGeeks
    </h1>
    <h3>
        Check if number is Infinity?
    </h3>
    <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>


Output:

 

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

Example: This example uses the approach discussed above. 

html




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>
        Check if number is Infinity?
    </h3>
    <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>


Output:

 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS