Number.isInteger(value) returns true when value is a JavaScript number with no fractional part. It does not convert strings to numbers.
console.log(Number.isInteger(5)); // true
console.log(Number.isInteger(5.5)); // false
console.log(Number.isInteger("5")); // false
Number.isInteger(value)The method returns a Boolean and performs no type coercion.
console.log(Number.isInteger(5)); // true
console.log(Number.isInteger(-5)); // true
console.log(Number.isInteger(0)); // true
console.log(Number.isInteger(5.5)); // false
console.log(Number.isInteger(-5.5)); // false
console.log(Number.isInteger(NaN)); // false
console.log(Number.isInteger(Infinity)); // false
JavaScript has one ordinary Number type for both integers and floating-point values. 5 and 5.0 represent the same numeric value.
console.log(Number.isInteger(5.0)); // true
const raw = "42";
console.log(Number.isInteger(raw)); // false
const value = Number(raw);
console.log(Number.isInteger(value)); // true
Number.isInteger() checks whether a Number has an integer value. Number.isSafeInteger() additionally checks whether that integer can be represented exactly within JavaScript's safe-integer range.
const n = Number.MAX_SAFE_INTEGER + 1;
console.log(Number.isInteger(n)); // true
console.log(Number.isSafeInteger(n)); // false
Because JavaScript Numbers use binary floating-point representation, a value that looks fractional in source code can sometimes round to an integer at very large magnitudes. Treat Number.isInteger() as a check of the stored Number value, not a guarantee about the original text entered by a user.
function readInteger(raw) {
if (raw.trim() === "") return null;
const value = Number(raw);
return Number.isInteger(value) ? value : null;
}
See NaN, numeric validation, and factors exercise.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.