Input validation
Validate numeric input before performing operations that require whole-number values.
JavaScript · Function
Number.isInteger() tests whether a value is a Number representing an integer. It does not coerce non-number values and returns false for NaN and infinities.
Call Number.isInteger(value) to test whether value is a Number representing an integer. It returns true for integral numeric values, including values such as 5.0, and false for non-numbers, non-integral numbers, NaN, and infinities; due to floating-point precision, some imprecise large values can still be classified as integers.
Number.isInteger(value)| Name | Type | Description |
|---|---|---|
value | any | The value to be tested for being an integer. |
A boolean: true if the given value is an integer; otherwise false.
const values = [5, 5.1, "5", NaN, 2 ** 53];
for (const value of values) {
console.log(value, Number.isInteger(value));
}Checks numbers without coercing strings or other non-number values.
Validate numeric input before performing operations that require whole-number values.
Reject fractional, string, boolean, NaN, or infinite values in API parameters.
Check whether a value can be treated as an integer before converting it to BigInt.
Number.isSafeInteger()Number.isFinite()Number.isNaN()Number.MAX_SAFE_INTEGER