DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

2 1

Number.isInteger() – JavaScript

ItsMyCode |

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)

Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output

true
false
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output

false
false
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output

true
true
true
false
true
false
Enter fullscreen mode Exit fullscreen mode

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'))

Enter fullscreen mode Exit fullscreen mode

Output

false
false
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output

false
false
false
false
false
Enter fullscreen mode Exit fullscreen mode

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;
};
Enter fullscreen mode Exit fullscreen mode

Supported Browsers:

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

The post Number.isInteger() – JavaScript appeared first on ItsMyCode.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay