How to check whether a number is finite or not in JavaScript?


In this article, we are going to discuss how to check whether a number is finite or not with suitable examples in JavaScript.

To check whether a number is finite or not in JavaScript, there is an inbuilt method which is isFinite() method. This method returns true if the number is a finte number. If the number is not a dfinite number, this method returns false.

To get a better understanding let’s look into the syntax and usage of isFinite() method in JavaScript.

Syntax

The syntax to check whether a number is finite or not is −

isFinite(value)

Where, value is the value that is to be checked whether it is finite or not. It can be any primitive data type. The return value is either true or false.

Example 1

This is an example program to write a function that checks a number whether it is finite or not.

<html>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>To check whether a number is finite or not in JavaScript?</title>
</head>
<body style="text-align: center;">
   <p>To check whether a number is finite or not in JavaScript</p>
   <p>Whether a function returns a finite number or not</p>
   <p id="result"></p>
   <script>
      function Division(a){
         if(isFinite(1/a)){
            return "1/"+a+" is a finite number";
         }
         else{
            return "1/"+a+" is not a finite number";
         }
      }
      document.getElementById('result').innerHTML = Division(770)+'<br/>'+Division(0);
   </script>
</body>
</html>

The output for the above example program is −

Example 2

This is an example program for which isFinite() method returns only true.

<html>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>To check whether a number is finite or not in JavaScript?</title>
</head>
<body style="text-align: center;">
   <p>To check whether a number is finite or not in JavaScript</p>
   <p>In this program, we will discuss the examples where the isFinite() method returns true.</p>
   <p id="result"></p>
   <script>
      var a = isFinite(12);
      var b = isFinite(-12);
      var c = isFinite(0);
      var d = isFinite(1.2);
      var e = isFinite(2000*3000);
      document.getElementById('result').innerHTML = 'isFinite(12) : '+a+'<br/>'+ 'isFinite(-12) : '+b+'<br/>'+'isFinite(0) : '+c+'<br/>'+ 'isFinite(1.2) : '+d+'<br/>'+'isFinite(2000*3000) : '+e+'<br/>';
   </script>
</body>
</html>

On executing the above code, the below output is generated.

Example 3

This is an example program for which the isFinite() method returns only false.

<html>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>To check whether a number is finite or not in JavaScript?</title>
</head>
<body style="text-align: center;">
   <p>To check whether a number is finite or not in JavaScript</p>
   <p>In this program, we will discuss the examples where the isFinite() method returns false.</p>
   <p id="result"></p>
   <script>
      var a = isFinite(NaN);
      var b = isFinite("hello");
      var c = isFinite(22 / 0);
      var d = isFinite("01/01/2001");
      document.getElementById('result').innerHTML = 'isFinite(NaN) : ' + a + '<br/>' + 'isFinite("hello") : ' + b + '<br/>' + 'isFinite(22/0) : ' + c + '<br/>' + 'isFinite("01/01/01") : ' + d;
   </script>
</body>
</html>

On executing the above code, the below output is generated.

Updated on: 09-Dec-2022

284 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements