Number.isInteger() – How to Check If a Number Is an Integer
Number.isInteger() checks if its argument is an integer. If so, it returns true
. Otherwise, the method returns false
.
Syntax of the Number.isInteger()
Method
Number.isInteger()
accepts only one required argument. Here is the syntax:
Number.isInteger(value);
The value
argument represents the item you wish to test if it is an integer.
note
- Integers are numbers without decimals. In other words, an integer can be negative, zero, or positive numbers. However, they do not include fractions (decimals).
- Some decimal numbers suffer from loss of precision in JavaScript. Therefore,
Number.isInteger()
will returntrue
for numbers like5.0000000000000001
and4500000000000000.1
.
Example 1: Is 578
an Integer?
Number.isInteger(578);
// The invocation above will return: true
Example 2: Is 0.99
an Integer?
Number.isInteger(0.99);
// The invocation above will return: false
Example 3: Is "test"
an Integer?
Number.isInteger("test");
// The invocation above will return: false
Example 4: Is Negative 300
an Integer?
Number.isInteger(-300);
// The invocation above will return: true
Example 5: Is Zero an Integer?
Number.isInteger(0);
// The invocation above will return: true
Example 6: Is Math.PI
an Integer?
Number.isInteger(Math.PI);
// The invocation above will return: false
Overview
This article discussed what Number.isInteger()
is. We also used examples to see how it works.