Javascript Reference - JavaScript isFinite() Function








The isFinite() function determines whether a number is a finite, legal number.

This function returns false if the value is +infinity, -infinity, or NaN (Not-a-Number), otherwise it returns true.

Browser Support

isFinite Yes Yes Yes Yes Yes

Syntax

isFinite(value)

Parameter Values

Parameter Description
value Required. The value to be tested




Return Value

A Boolean. Returns false if the value is +infinity, -infinity, or NaN, otherwise it returns true.

Example

The following code shows how to use isFinite function to check if a number is finit.


if (isFinite(Number.NEGATIVE_INFINITY)) {
   console.log("Number is finite.");
} else {
   console.log("Number is infinite.");
}

The code above generates the following result.





Example 2

The following code shows how to check whether a number is a finite, legal number.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from  w w  w.ja  v a  2  s . com-->
<p id="demo"></p>

<script>
function myFunction() {
    var a = isFinite(123) + "<br>";
    var b = isFinite(-1.23) + "<br>";
    var c = isFinite(5-2) + "<br>";
    var d = isFinite(0) + "<br>";
    var e = isFinite("Hello") + "<br>";
    var f = isFinite("2012/12/12") + "<br>";

    var res = a + b + c + d + e + f;
    document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

The code above is rendered as follows: