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.
Number.isInteger(value)
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
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 withNumber(text)orparseFloat()before testing form input. - Floating-point artifacts.
0.1 * 3 / 0.3is1.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)istrue, but the value may not be exact. UseNumber.isSafeInteger()to ensure exactness. - Not for BigInt.
Number.isInteger(5n)isfalse; BigInts are always integers, so check withtypeof value === "bigint". - Negative zero counts as an integer.
- Alternative checks:
Number.isInteger(x)is the same asNumber.isFinite(x) && Math.floor(x) === x. - Empty string pitfall:
Number("")andNumber(" ")are0, which is an integer. Check for an empty string yourself.
Browser support
Supported in all current browsers; it arrived with ES2015.