JavaScript - isFinite() method

revision:


Category : number

The Number.isFinite() static method determines whether the passed value is a finite number — that is, it checks that a given value is a number, and the number is neither positive Infinity, negative Infinity, nor NaN.

Syntax :

        Number.isFinite(value)
    

Parameters:

value : required. The value to be tested for finiteness.

Examples:

        console.log(Number.isFinite(1 / 0));
        // Expected output: false

        console.log(Number.isFinite(10 / 5));
        // Expected output: true

        console.log(Number.isFinite(0 / 0));
        // Expected output: false
    

Practical examples

example: usig the isFinite() method

code:
                    <div>
                        <p id="finite-1"></p>
                        <p id="finite-2"></p>
                        <p id="finite-3"></p>
                        <p id="finite-4"></p>
                        <p id="finite-5"></p>
                        <p id="finite-6"></p>
                        <p id="finite-7"></p>
                    </div>
                    <script>
                        document.getElementById("finite-1").innerHTML = "Number.isFinite(Infinity) : " + Number.isFinite(Infinity); // false
                        document.getElementById("finite-2").innerHTML = "Number.isFinite(NaN) : " + Number.isFinite(NaN); // false
                        document.getElementById("finite-3").innerHTML = "Number.isFinite(-Infinity) : " + Number.isFinite(-Infinity); // false
                        document.getElementById("finite-4").innerHTML = "Number.isFinite(0) : " + Number.isFinite(0); // true
                        document.getElementById("finite-5").innerHTML = "Number.isFinite(2e64) : " + Number.isFinite(2e64); // true
                        document.getElementById("finite-6").innerHTML = "Number.isFinite(123) : " + Number.isFinite(123); // true 
                        document.getElementById("finite-7").innerHTML = "Number.isFinite('123') : " + Number.isFinite('123'); // false
                    </script>