Number.isInteger()
JavaScript has no integer type, so this asks about the value rather than the type: is this double a whole number? Its companion isSafeInteger adds the requirement that the value be exactly representable.
Demo
Whole numbers are true, fractions false, and non-numeric text false. What the demo cannot easily show is the no-coercion rule that matters most: Number.isInteger('5') — the STRING five — is false, because a string is not a number no matter what it looks like. The auto input here converts numeric-looking text to a real number before the call, which is exactly the conversion your own code has to do first. Also worth knowing: 5.0 is true, since JavaScript stores it identically to 5.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| value | any | yes | Any value. Non-numbers return false without conversion. Infinity and NaN are false; 5.0 is true, because it is indistinguishable from 5. |
Return value
boolean — True if the value is a number with no fractional part. No coercion — a numeric STRING is false, because it is not a number at all.
Common patterns
const n = Number(input); if (!Number.isInteger(n)) reject();
const ok = Number.isInteger(i) && i >= 0 && i < arr.length;
if (!Number.isSafeInteger(id)) throw new Error("id too large");
Examples
Pitfalls
Number.isInteger('5')
Number.isInteger(Number('5'))
Number.isInteger(5.0)
'5.0'.includes('.')
Number.isInteger(2 ** 53 + 2)
Number.isSafeInteger(2 ** 53 + 2)
'5' % 1 === 0
Number.isInteger('5')
When to use
- Validating that a converted value is whole
- Guarding array indices and counts
- Rejecting fractional input where only whole units make sense
- With isSafeInteger, checking ids have not lost precision
- The value is a string → convert with Number first
- You need the whole part → Math.trunc or Math.floor
- Large exact integers → BigInt
- Checking for any number → Number.isFinite
Notes
FAQ
Because the argument is a string, and the method performs no conversion — that is the whole design. The global-style predicates that DO convert, such as isNaN, are exactly the ones ES2015 added replacements for.
Number.isInteger(Number('5')); // true