Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Number and BigInt

Number.isInteger()

Reference for Number.isInteger(): checking whether a value is a whole number, why strings and Infinity fail, and how it differs from isSafeInteger().

Number.isInteger() returns true when its argument is a number with no fractional part. It does not convert the argument: a string such as "5" is not a number, so the answer is false.

JavaScript has only one numeric type for ordinary numbers, so 5 and 5.0 are the same value, and both are integers.

JavaScript
Number.isInteger(value)

Try it

JS What counts as an integer

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Validating quantities

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
Number.isInteger(value)
Name Description
value The value to test. Any type is accepted.

Return value: true if value is of type number, finite, and equal to its own integer part; otherwise false.

Exceptions: none.

Usage notes

  • No coercion. Unlike the global isNaN(), this method looks at the type first. Convert with Number(text) or parseFloat() before testing form input.
  • Floating-point artifacts. 0.1 * 3 / 0.3 is 1.0000000000000002, not an integer, even though maths says it should be one. Round or use a tolerance when the result of decimal arithmetic matters.
  • Huge numbers are integers. Above 2^53 every double is a whole number, so Number.isInteger(1e300) is true, but the value may not be exact. Use Number.isSafeInteger() to ensure exactness.
  • Not for BigInt. Number.isInteger(5n) is false; BigInts are always integers, so check with typeof value === "bigint".
  • Negative zero counts as an integer.
  • Alternative checks: Number.isInteger(x) is the same as Number.isFinite(x) && Math.floor(x) === x.
  • Empty string pitfall: Number("") and Number(" ") are 0, which is an integer. Check for an empty string yourself.

Browser support

Supported in all current browsers; it arrived with ES2015.