Computer >> Computer tutorials >  >> Programming >> Javascript

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


Javascript has provided isFinite() method to check whether the given entity is a legal number or not. If the given entity is a number, regardless of string for instance "123", this method will result in boolean true, else returns false. Let's discuss it in a nutshell.

syntax

isFinite(value);

This method takes a value as a parameter and returns boolean true if the value passed is a number else returns a boolean false.

Example-1

In the following example, numbers were sent into the method isFinite() as parameters and the result is displayed in the output.

<html>
<body>
<p id = "number"></p>
<script>
   var a = isFinite(567) + "</br>";
   var b = isFinite(-9.23) + "</br>";
   var c = isFinite(0) + "</br>";
   var d = isFinite(6-7) + "</br>";
   var bol = a + b + c + d;
   document.getElementById("number").innerHTML = bol;
</script>
</body>
</html>

Output

true
true
true
true

Example-2

In the following example, strings and date were passed as parameters. This method accepts the number strings as a number and returns true as output. 

<html>
<body>
<p id = "number"></p>
<script>
   var x = isFinite("123") + "<br>";
   var y = isFinite("string") + "<br>";
   var z = isFinite("2019/08/06");
   var res = x + y + z ;
   document.getElementById("number").innerHTML = res;
</script>
</body>
</html>

Output

true
false
false