Skip to main content

How to Check if a Number Is a Whole Number in JavaScript with Number.isInteger

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Number.isInteger(value) gives true when the value is a number with no fractional part, such as 500, and false for anything else, including the string '500'. It doesn't convert its argument, so it also tells you the value really is a number.

Syntax​

Syntax
Number.isInteger(<value>)
PartWhat it does
Number.isIntegerA static method of Number: you call it on Number itself.
<value>Any value. Anything that isn't a number gives false, and it never throws.

MDN's Number.isInteger reference: "If the target value is an integer, return true, otherwise return false. If the value is NaN or Infinity, return false."

The Example​

The example checks 500, 500.0, 500.5, the string '500', NaN and Infinity.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which logs Number.isInteger for 500, 500.0, 500.5, the string 500, NaN and Infinity.

main.js
console.log(Number.isInteger(500));
console.log(Number.isInteger(500.0));
console.log(Number.isInteger(500.5));
console.log(Number.isInteger('500'));
console.log(Number.isInteger(NaN));
console.log(Number.isInteger(Infinity));

Step 2: Run It​

Terminal
node main.js
Output
true
true
false
false
false
false

A zsh terminal where node main.js prints true, true, false, false, false and false.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • 500 is true, a whole number.
  • 500.0 is true as well. JavaScript has one number type, and 500.0 is the same number as 500.
  • 500.5 is false: it has a fractional part.
  • The string '500' is false. Number.isInteger doesn't convert strings, so text that looks like a number doesn't pass.
  • NaN and Infinity are false, even though typeof says both are numbers.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting '500' from a form or a URL to passIt's a string, so the check failsConvert it first: Number.isInteger(Number('500')) is true
Calling isInteger(5) on its ownReferenceError: isInteger is not definedCall it on Number: Number.isInteger(5)
Using typeof value === 'number' to mean "a usable whole number"500.5, NaN and Infinity pass tooUse Number.isInteger(value)
Checking a BigInt such as 10nfalse: a BigInt isn't a numberCheck with typeof value === 'bigint'

Troubleshooting​

SymptomLikely causeFix
A number you typed with decimals passes, such as 5.0000000000000001That many digits are beyond what a JavaScript number holds, so it's stored as 5Round or validate the input as text first
Huge numbers pass, but arithmetic on them is wrongAbove 2 ** 53, numbers can't hold every whole valueUse Number.isSafeInteger

FAQ​

What's the difference between Number.isInteger and Number.isSafeInteger?​

Number.isSafeInteger also requires the number to be small enough to be exact. Number.isInteger(2 ** 53) is true, but Number.isSafeInteger(2 ** 53) is false.

Does Number.isInteger accept negative numbers?​

Yes. Any whole number counts, negative ones and 0 included.

Conclusion​

Number.isInteger is a one-call check that a value is a real number with no fractional part. Convert input from text first, and use Number.isSafeInteger when the number must also be exact.

Additional Resources​