Number.isInteger()

Checks whether a value is an integer.

Returns
true or false.
On this page

Example

#
JavaScript
console.log(Number.isInteger(5))
console.log(Number.isInteger(5.0))
console.log(Number.isInteger(5.5))
console.log(Number.isInteger('5'))
console.log(Number.isInteger(Infinity))
Console
true
true
false
false
false

Definition and usage

#

The Number.isInteger() function returns true if the value is a number with no fractional part. Numbers written with zeros after the period, like 5.0, count as integers too.

It doesn’t convert types: the string "5" isn’t a number, so the result is false. For NaN and Infinity the result is also false.

It’s useful for validating data, such as the quantity in a cart, an age, or a page number, which all need to be whole numbers.

Syntax

#
Syntax
Number.isInteger(value)

Parameters

#
  • value

    any value

    The value being checked.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Convert a form field’s value with Number() first, then check Number.isInteger().
  • Number.isSafeInteger() checks whether a number fits in the safe range.

Related entries

#

Spotted a mistake or missing an example? Write to us.