# Number.isInteger() – JavaScript

In JavaScript, `Number.isInteger()` method checks whether the passed value is an integer or not. The method `isInteger()` returns true if the provided value is an integer or type number. Otherwise, it returns false.

## Syntax

``````Number.isInteger(value)
``````

Parameters:

value – The `isInteger()` method accepts a single parameter value that needs to be tested, and its a required parameter

Return Value – The method `Number.isInteger()` returns a boolean value, i.e,. returns true if the target value passed is an integer. Otherwise, it returns false. If the value is `NaN `or `Infinity`, it returns false.

The `isInteger()` method returns true if you pass floating-point numbers that can be represented as an integer.

Let’s take few examples that illustrate the `Number.isInteger() `method in JavaScript.

## Passing positive number as an argument

If you pass a positive value to the function as an argument, then it returns true. If the value passed is not of integer type, then it returns false.

``console.log(Number.isInteger(77))``

Output

``true``

## Passing negative number as an argument

If you pass a negative value to the function as an argument, then it returns true. If the value passed is not of integer type, then it returns false.

``````console.log(Number.isInteger(-77))
console.log(Number.isInteger(-1.3))``````

Output

``````true
false``````

## Passing zero as an argument

If you pass zero as an argument to the `Number.isInteger()` method, it returns true as 0 is an valid integer.

``console.log(Number.isInteger(0))``

Output

``true``

## Passing floating-point number as an argument

If you pass a floating or decimal value to the function as an argument, it returns false as it’s not a valid integer.

``````console.log(Number.isInteger(34.33))
console.log(Number.isInteger(-1.4))``````

Output

``````flase
false``````

## Passing floating-point number as an argument representing integer format

If you pass a floating or decimal value representing an integer, the `isInteger()` method returns true as it can parse as a valid integer.

``````console.log(Number.isInteger(5.0)) //positive float representing as integer
console.log(Number.isInteger(5.0000000)) //positive float representing as integer
console.log(Number.isInteger(-12.0000000)) //negative float representing as integer
console.log(Number.isInteger(-5.000000000000001)) //negative float point number
console.log(Number.isInteger(-5.0000000000000001)) //negative float point number
console.log(Number.isInteger(-1.4))``````

Output

``````true
true
true
false
true
false``````

## Passing string as an argument

If you pass a string as an argument to the `Number.isInteger()` method, it will return false. Even if you pass integer in string format, the method will return false as it cannot parse the string value.

``````console.log(Number.isInteger('hello'))
console.log(Number.isInteger('55'))
``````

Output

``````false
false``````

## Passing NaN, Infinity, Boolean as an argument

If you pass a `NaN`, `Infinity`, `Boolean `as an argument to the `Number.isInteger() `method, it will return false.

``````console.log(Number.isInteger(NaN))
console.log(Number.isInteger(Infinity))
console.log(Number.isInteger(-Infinity))
console.log(Number.isInteger(true))
console.log(Number.isInteger(false))``````

Output

``````false
false
false
false
false``````

## Polyfill for Number.isInteger()

If you are looking for a Polyfill, you could use the code below if your browser is not supported.

``````Number.isInteger = Number.isInteger || function(value) {
return typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) === value;
};``````

Supported Browsers:

• Edge: 12
• Internet Explorer: Not Supported
• Firefox: 16
• Apple Safari: 09
• Opera: 21

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

##### You May Also Like ## How to get query string values using JavaScript?

Here is a very simple method to get query string values using javascript. Below code snippet can be used to fetch the query string value in the URL when the… ## How to detect Page Editor mode in Sitecore through code

f you are a Sitecore developer then definitely in many instances you would need to show or hide some components in the Page Editor mode. So How to detect Page… ## Javascript code to disable mouse right click

You could use Javascript code to disable mouse right click on the webpage. This article demonstrates two methods to disable the context menu with the right click of a mouse… ## How to create a JavaScript Dictionary and add key value pairs

Dictionary objects are very useful in any programming language. In this article let’s see how to create a JavaScript Dictionary and add key-value pairs. Many programming languages like C# and… ## Measure JavaScript Execution time console.time() and console.timeEnd()

Debugging the JavaScipt code to identify performance issue is a very tedious task. There are built-in JavaScript methods which you could use to measure the performance and the time taken… ## Detect If Internet Connection Exists with JavaScript

There are times when the user might be browsing and loose the internet connectivity all of a sudden or user might be doing an offline browsing which comes basically from HTML…