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

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:
Checking if number is equal to Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY .
Example 1: 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;"      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:
JavaScript-Misc
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to Open URL in New Tab using JavaScript ?
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript