Skip to content

Number.isInteger()

Why Number.isInteger ?

The Number.isInteger () method checks whether the value to be checked is an integer. Returns “true” if the assigned value is an integer. Returns false if the number is not an integer.

Syntax:

Number.isInteger(value)

  • value (required): value to be checked.

JavaScript Number.isInteger Examples

Example 1

        let val1 = Number.isInteger(1234);
        let val2 = Number.isInteger(1.55);
        let val3 = Number.isInteger(-8.99);
        let val4 = Number.isInteger(0);
        let val5 = Number.isInteger(null);
        let val6 = Number.isInteger("394");
        let val7 = Number.isInteger("truecodes.org");
        let val8 = Number.isInteger(2000 / 1000);
        let val9 = Number.isInteger(Infinity);
        let val10 = Number.isInteger(-Infinity);
        let val11 = Number.isInteger(0 / 0);
        let val12 = Number.isInteger(function () { });
        let val13 = Number.isInteger(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
false
false
true
false
false
false
true
false
false
false
false
false


Example 2

Returns a positive integer true. If the value to be checked is a negative integer, it returns true.

        let example1 = Number.isInteger(123);
        console.log(example1);

        let example2 = Number.isInteger(-123);
        console.log(example2);

output:

true
true


Example 3

Returns false if the value to be checked is not a positive integer. If the value to be checked is a negative integer, it returns false.

        let example1 = Number.isInteger(12.3);
        console.log(example1);

        let example2 = Number.isInteger(-12.3);
        console.log(example2);

output:

false
false


Polyfill

Number.isInteger = Number.isInteger || function(value) {
  return typeof value === 'number' && 
    isFinite(value) && 
    Math.floor(value) === value;
};

Source:


Browser Support

Chromeyes
Edgeyes
Firefox16
Internet Explorerno
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android16
Opera Androidyes

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: