user
How to check if a variable is an integer in JavaScript?
alphonsio

In JavaScript, to check if a variable is an integer, use:

Number.isInteger(value)

Example:

let x = 42;
console.log(Number.isInteger(x)); // true

let y = 3.14;
console.log(Number.isInteger(y)); // false

let z = "10";
console.log(Number.isInteger(z)); // false (it's a string, not a number)

Common Gotcha:

Even if a variable looks like a number (e.g., "5"), it's still a string unless explicitly converted. To be safe:

let n = parseInt(someValue, 10);
if (Number.isInteger(n)) {
  // safe to use as an integer
}