Skip to content
Advertisements

Number.isFinite()

Why Number.isFinite ?

The Number.isFinite () method checks whether the specified value is finite. Returns “true” if the value to be checked is finite. Returns false if the value to be checked is not finite. Number.isFinite () does not convert values to a Number and does not return true for any value that is not of Number.

The Number.isfinite () method differs from the isfinite () function because this method does not force the parameter to a number and does not return true for any value that does not have a type number. Number.isFinite () is different from global isFinite (). The global isFinite () function converts the tested value to a Number, then tests it.

Syntax:

Number.isFinite(value)

  • value (required): The value to be tested.

JavaScript Number.isFinite Examples

Example 1

        let val1 = Number.isFinite(1234);
        let val2 = Number.isFinite(1.55);
        let val3 = Number.isFinite(-8.99);
        let val4 = Number.isFinite(0);
        let val5 = Number.isFinite(null);
        let val6 = Number.isFinite("394");
        let val7 = Number.isFinite("truecodes.org");
        let val8 = Number.isFinite(2000/1000);
        let val9 = Number.isFinite(Infinity);
        let val10 = Number.isFinite(-Infinity);
        let val11 = Number.isFinite(0 / 0);
        let val12 = Number.isFinite(function(){});
        let val13 = Number.isFinite(2019 / 02 / 03);

        console.log(val1);
        console.log(val2);
        console.log(val3);
        console.log(val4);
        console.log(val5);
        console.log(val6);
        console.log(val7);
        console.log(val8);
        console.log(val9);
        console.log(val10);
        console.log(val11);
        console.log(val12);
        console.log(val13);

output:

true
true
true
true
false
false
false
true
false
false
false
false
true


Example 2

        function div(x) {
            if (Number.isFinite(1000 / x)) {
                return 'Number is NOT Infinity.';
            }
            return 'Number is Infinity!';
        }

        console.log(div(0));

        console.log(div(1));

output:

Number is Infinity!
Number is NOT Infinity.


Polyfill

if (Number.isFinite === undefined) Number.isFinite = function(value) {
    return typeof value === 'number' && isFinite(value);
}

Source:


Browser Support

Chrome19
Edgeyes
Firefox16
Internet Explorerno
Opera15
Safari9
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android16
Opera Androidyes
Advertisements

1 Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: