Number.isInteger()
Number.isInteger(value) Boolean · Updated March 13, 2026 · Number The Number.isInteger() method determines whether the passed value is an integer. This is useful for validating user input, checking API responses, or ensuring calculations produce whole numbers. In JavaScript, integers are numbers without a fractional component, and this method provides a reliable way to test for them.
Syntax
Number.isInteger(value)
Parameters
| Parameter | Type | Description |
|---|---|---|
value | any | The value to check |
Return Value
Returns true if the value is an integer (a whole number without any fractional component). Returns false for non-integers, non-number types, and special values like Infinity, -Infinity, and NaN.
Examples
Basic Usage
Number.isInteger(42); // true
Number.isInteger(0); // true
Number.isInteger(-10); // true
Number.isInteger(1000); // true
Floating Point Numbers
Floating point numbers return false because they have a fractional component:
Number.isInteger(3.14159); // false
Number.isInteger(1.5); // false
Number.isInteger(0.1); // false
Number.isInteger(-2.5); // false
Edge Cases: Integer-Like Floats
Interestingly, JavaScript treats some floating point numbers as integers:
// These are technically floats but are considered integers by isInteger
Number.isInteger(1.0); // true (1.0 equals 1)
Number.isInteger(42.0); // true
Number.isInteger(-5.0); // true
This is because JavaScript doesn’t distinguish between 1 and 1.0 at the language level — they’re both the integer value 1.
Special Values
Special numeric values return false:
Number.isInteger(Infinity); // false
Number.isInteger(-Infinity); // false
Number.isInteger(NaN); // false
Non-Number Types
Non-number types always return false:
Number.isInteger('42'); // false (string)
Number.isInteger('hello'); // false
Number.isInteger(undefined); // false
Number.isInteger(null); // false
Number.isInteger(true); // false (boolean)
Number.isInteger(false); // false
Number.isInteger({}); // false (object)
Number.isInteger([]); // false (array)
Common Patterns
Form Validation
Validating that user input is a whole number:
function validateAge(input) {
if (!Number.isInteger(input)) {
return 'Age must be a whole number';
}
if (input < 0 || input > 150) {
return 'Age must be between 0 and 150';
}
return 'Valid';
}
validateAge(25); // 'Valid'
validateAge(25.5); // 'Age must be a whole number'
validateAge(-5); // 'Age must be between 0 and 150'
Array Index Validation
Ensuring an index is a valid integer:
function getItem(arr, index) {
if (!Number.isInteger(index)) {
throw new Error('Index must be an integer');
}
if (index < 0 || index >= arr.length) {
throw new Error('Index out of bounds');
}
return arr[index];
}
const items = ['a', 'b', 'c'];
getItem(items, 1); // 'b'
getItem(items, 1.5); // Error: Index must be an integer
Loop Counter Validation
function createArray(length) {
if (!Number.isInteger(length) || length <= 0) {
throw new Error('Length must be a positive integer');
}
return new Array(length).fill(0);
}
createArray(5); // [0, 0, 0, 0, 0]
createArray(3.5); // Error: Length must be a positive integer
Type Coercion Comparison
Unlike some other methods, Number.isInteger() does not coerce its argument:
// Comparison with the global approach ( Number() conversion)
Number.isInteger(Number('42')); // true (after coercion to 42)
Number.isInteger('42'); // false (string, not coerced)
Number.isInteger(Number('3.14')); // false (after coercion to 3.14)
Number.isInteger('3.14'); // false (string)
Filtering Arrays
const mixed = [1, 2.5, 3, 4.5, 5, 6, 7.5];
const integers = mixed.filter(Number.isInteger);
// [1, 3, 5, 6]
const floats = mixed.filter(x => !Number.isInteger(x));
// [2.5, 4.5, 7.5]
Combined Validation
function processQuantity(value) {
if (typeof value !== 'number' || !Number.isFinite(value)) {
return { error: 'Must be a finite number' };
}
if (!Number.isInteger(value)) {
return { error: 'Must be an integer' };
}
if (value < 0) {
return { error: 'Must be non-negative' };
}
return { success: true, value };
}
processQuantity(10); // { success: true, value: 10 }
processQuantity(10.5); // { error: 'Must be an integer' }
processQuantity(Infinity); // { error: 'Must be a finite number' }
See Also
- Number.isFinite() — check if value is a finite number
- Number.isNaN() — check if value is NaN
- JSON.parse() — parse a JSON string