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:
- 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> |
chevron_right
filter_none
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> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- What is negative infinity in JavaScript?
- JavaScript | Infinity Property
- What is positive infinity in JavaScript?
- Check a number is Prime or not using JavaScript
- Substring check in JavaScript
- How to check a function is defined in JavaScript ?
- How to check object is an array in JavaScript?
- How to check a date is valid or not using JavaScript ?
- How to check if date is less than 1 hour ago using JavaScript ?
- JavaScript | Check if a variable is a string
- How to check if a string is html or not using JavaScript?
- JavaScript | Check the existence of variable
- How to check if a variable is an array in JavaScript?
- How to check whether an object exists in javascript ?
- How to check if an element has any children in JavaScript ?
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.



