The JavaScript Number.isInteger() method
The Number.isInteger() method returns true if a value is an integer — a whole number — and false otherwise. Number.isInteger(42) is true; Number.isInteger(42.5) is false. It does not convert types, so Number.isInteger("42") is false (it's a string).
Overview
Number.isInteger() answers a precise question: is this value a whole number? Number.isInteger(10) is true, Number.isInteger(10.5) is false. It's a clean way to validate that input is a count, an index, an ID, or anything that shouldn't have a fractional part.
A defining feature is that it does not coerce types. The value must actually be a number and a whole one. Number.isInteger("42") is false because a string isn't a number, and so are NaN, Infinity, true and null. That strictness is a feature — it won't be fooled by string digits the way the old global checks sometimes were.
It pairs with the other static Number validators — Number.isNaN() for the not-a-number check and Number.isFinite() for finite-number check. Together they give you reliable, coercion-free type guards. If you need to accept numeric strings, parse them first with parseInt() or Number(), then test.
Syntax
Number.isInteger(value)
Number.isInteger(42) // true
Number.isInteger(42.5) // false
Number.isInteger("42") // false (string, no coercion)
Number.isInteger(NaN) // false
Parameters
The Number.isInteger() method accepts the following parameters.
| Parameter | Description |
|---|---|
value |
The value to test. Returns true only if it is a number with no fractional part. |
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const values = [42, 42.5, '42', NaN, 100];
const lines = values.map(v =>
JSON.stringify(v) + ' -> ' + Number.isInteger(v)
);
document.getElementById('out').textContent = lines.join('\n');
</script>
Best practices
- Use it to validate counts, indices and IDs that must be whole numbers.
- Remember it doesn't coerce — pass a real number, or parse strings first with parseInt().
- Combine with Number.isNaN() and
Number.isFinite()for full numeric validation. - Prefer it over manual checks like
n % 1 === 0, which can mis-handle non-numbers.
Frequently asked questions
How do I check if a number is a whole number?
Number.isInteger(value), which returns true for whole numbers and false for decimals or non-numbers.Why is Number.isInteger("5") false?
"5" is a string, not a number. Number.isInteger() doesn't coerce types. Convert it first with Number("5").Does Number.isInteger() return true for NaN or Infinity?
false, as do booleans, strings and null.How is it different from checking n % 1 === 0?
Number.isInteger() also rejects non-numbers safely, whereas the modulo trick can give misleading results for strings or special values.