JavaScript Number isInteger() Method
The JavaScript Number isInteger() method is a static method used to determine whether the passed value is an integer or not. It returns a boolean value 'true' if the passed value is an integer. Otherwise, it returns 'false'.
In mathematics, integers are whole numbers that can be positive, negative, or zero, such as {1, 2, 3, 4, ..., 0, -1, -2, -3, ...}.
Syntax
Following is the syntax of JavaScript Number isInteger() method −
Number.isInteger(value)
Parameters
This method accepts a parameter named 'value', which is described below −
- value − The value to be checked for an integer.
Return value
This method returns 'true' if the passed value is an integer; 'false' otherwise.
Example 1
If we pass a positive value to this method, it will return 'true'.
In the program below, we use the isInteger() method to determine whether the passed value '10' is an integer or not.
<html>
<head>
<title>JavaScript isInteger() Method</title>
</head>
<body>
<script>
let num = 10;
document.write("Is number ", num, " is an Integer? ", Number.isInteger(num));
</script>
</body>
</html>
Output
The program mentioned above returns a boolean value 'true' for the value 10 as −
Is number 10 is an Integer? true
Example 2
If we pass null as an argument to this method, it returns 'false'.
Following is another example of the isInteger() method. We pass 'null' as an argument to this method to check whether it is an integer or not.
<html>
<head>
<title>JavaScript isInteger() Method</title>
</head>
<body>
<script>
let val = null;
document.write("Is '", val, "' is an Integer? ", Number.isInteger(val));
</script>
</body>
</html>
Output
Once the above program is executed, it will return 'false' in the output as −
Is 'null' is an Integer? false
Example 3
Let's pass 0 (zero) as an argument to the 'isInteger()' method to verify whether it is an integer or not.
<html>
<head>
<title>JavaScript isInteger() Method</title>
</head>
<body>
<script>
let val = 0;
document.write("Is '", val, "' is an Integer? ", Number.isInteger(val));
</script>
</body>
</html>
Output
The above program returns 'true' for value 0 as −
Is '0' is an Integer? true
Example 4
The following example demonstrates the usage of the isInteger() method.
<html>
<head>
<title>JavaScript isInteger() Method</title>
</head>
<body>
<script>
document.write("Is the result of 15/0 is an Inetger? ", Number.isInteger(10/0));
</script>
</body>
</html>
Output
The result of ( 15/0 ) is 'infinity', so the above program returns 'false' for the result of this value −
Is the result of 15/0 is an Inetger? false
Example 5
The example shown below will demonstrate the real-time usage of the JavaScript Number isInteger() method.
<html>
<head>
<title>JavaScript isInteger() Method</title>
</head>
<body>
<script>
//declaring function
function print(x, y){
if(Number.isInteger(x/y)){
document.write("Ok")
}
else{
document.write("Not ok");
}
}
let x = 10;
let y = 5;
document.write("x = ", x);
document.write("<br> y = ", y);
//call the function
document.write("<br>Result1 = ")
print(x, y);
//output will be 'Ok',
let n1 = 14.5
let n2 = 5;
document.write("<br> n1 = ", n1);
document.write("<br> n2 = ", n2);
//call the function
document.write("<br>Result2 = ")
print(n1, n2);
//output will be 'Not ok', because decimal number can't be consider as an integer value
</script>
</body>
</html>
Output
The program displays the statements 'Ok' and 'Not ok' based on the specified condition −
x = 10 y = 5 Result1 = Ok n1 = 14.5 n2 = 5 Result2 = Not ok