JavaScriptNumbers and Math
Number. isInteger()
Checks whether a value is an integer.
- Returns
trueorfalse.
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 checkNumber.isInteger(). Number.isSafeInteger()checks whether a number fits in the safe range.
Related entries
#- Number.isNaN()Checks whether a value is exactly NaN.
- Number.parseInt()Converts the start of a string into an integer in a given numeral system.
- Math.trunc()Cuts off the fractional part of a number, leaving the integer part.
- BigIntA type for exact calculations on arbitrarily large integers. Literals end with the letter n.
Spotted a mistake or missing an example? Write to us.