Home > shorts > Check if a variable is an Integer in JavaScript

Check if a variable is an Integer in JavaScript


The Number.isInteger() method determines whether the passed value is an integer.

Number.isInteger(0);            // ✅ true
Number.isInteger(10);           // ✅ true
Number.isInteger(5.0);          // ✅ true

Number.isInteger(5.1);          // ❌ false
Number.isInteger(Math.PI);      // ❌ false
Number.isInteger(NaN);          // ❌ false
Number.isInteger(Infinity);      // ❌ false
Number.isInteger("10");         // ❌ false
Number.isInteger(true);         // ❌ false
Number.isInteger([1]);          // ❌ false

If the target value is an integer, return true, otherwise return false. If the value is NaN or Infinity, return false. The method will also return true for floating point numbers that can be represented as integer.

© 2021, Andrew Losseff