JavaScript Number.isInteger() Method
In JavaScript, the Number.isInteger()
is used to check the specified value for a parameter of this method is an integer value or not.
Here, Integer values mean the whole number that doesn’t contain the values after the decimal point, which could be a positive or negative integer.
Syntax
let num = 10l;
Number.isInteger(num);
Parameters
num |
The Number.isInteger() checks for the num whether it is an integer or not. |
Returns
It returns true
or false
Boolean values based on whether the number is an integer or non-integer.
Example Codes
Pass Various Input Values for Number.isInteger()
In JavaScript, there are two types of numerical values. One is the whole number, which means an integer, and another is a float number, which means non-integers.
In a code snippet below, we will take the different numeric values and observe the output we get from the Number.isInteger()
method.
let value1 = 10.2;
let value2 = 10;
let value3 = 0;
console.log(Number.isInteger(value1));
console.log(Number.isInteger(value2));
console.log(Number.isInteger(value3));
Output:
false
true
true
Use the Number.isInteger()
Method With if-else
Block
The following code example uses the if-else
block with the Number.isInteger()
method. We have taken two numbers and will check if both numbers are integers.
Then, we will multiply both input values if they are integers; otherwise, inform the user that the given numbers are not integers. It is a simple demonstration of how users can use the Number.isInteger()
method in real-world programming.
See the following code.
let num1 = 0.2;
let num22 = 2;
if(Number.isInteger(num1) && Number.isInteger(num2)){
console.log("Multiplication of Integers is " + num1*num2);
}else{
console.log("Both numbers are not Integers.");
}
Output:
Both numbers are not Integers.