How would you check if a number is an integer?

Technology CommunityCategory: JavaScriptHow would you check if a number is an integer?
VietMX Staff asked 3 years ago

A very simply way to check if a number is a decimal or integer is to see if there is a remainder left when you divide by 1.

function isInt(num) {
  return num % 1 === 0;
}

console.log(isInt(4)); // true
console.log(isInt(12.2)); // false
console.log(isInt(0.3)); // false