isInteger JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

The isinteger javascript method of the Number object is used to determine if the value passed as the parameter to it is an integer or not. It is a method in Javascript that returns true if the values passed to it is an integer; otherwise, it returns false.

If the value is NaN or Infinity, the isInteger method will return false. The method will also return true for floating-point numbers that can be represented as integers.

Syntax of isInteger() in Javascript

Here Number is the javascript object and the value is the parameter in isInteger javascript.

Parameters of isInteger() in Javascript

  • Value: It represents the value that is being tested to check if it is an integer or not. It can be a number, string, expression, etc. in isInteger javascript.

Return Value of isInteger() in Javascript

  • Boolean: It returns the boolean value true in case the value passed to it is an integer. If the value is not an integer, it will return false.

Note: If the value passed to the javascript isInteger method is a non-numeric value, it will return the value false. This happens since the javascript isInteger method does not do the type conversion of the value before checking if it is an integer.

For example:

In the above expression, the isInteger javascript would return false since '5' is a string.

Example

Output:

Explanation of the example:

In the above example, we are given a function example that takes two parameters a and b and returns the value b/a.

In the first case, the values of a and b are 5 and 2 respectively, thus the value of b/a would be 0.4, which is not an integer. Thus the isInteger javascript would output false.

In the second case the values of a and b are 5 and 10 respectively, thus the value of b/a would be 2, which is an integer. Thus the isInteger javascript would output true.

What is isInteger() in Javascript?

In programming, we often encounter situations where we need to know the exact type of a number. For e.g. imagine we are taking student data through a form, in this case, we would need to validate if the fields like exam score, roll no, etc are integer values.

So, the question is How do we do that in Javascript?

The isInteger() method in javascript is used to check if the value passed to it is an integer or not. It is the method of Javascript Number object that has been added during ES6 revision.

Example 1. Passing a Negative Number as an Argument

Output:

Explanation of the example:

In the above example, the javascript isInteger method is passed the value -5, which is a negative value. The Number.isInteger(-5) would return true, thus the code outputs 'It is an integer' .

Example 2. Passing a Positive Number as an Argument

Output:

Explanation of the example:

In the above example, in the first case, the javascript getIntegerRemainder method is passed the arguments 10 and 5. Since the value of 10/5 will be 2 which is an integer thus Number.isInteger(10/5) will hold true, and the function will return 2.

In the second case, the javascript getIntegerRemainder method is passed the arguments 10 and 3. Since the value of 10/3 will be 3.33, which is not an integer thus Number.isInteger(10/3) will hold false and the function will return The remainder is not an integer.

Example 3. Passing Zero as an Argument

Output:

Explanation of the example:

In the above example, the javascript isInteger method is passed the value 0. The Number.isInteger(0) would return true, thus the code outputs 'It is an integer' .

Example 4. Passing NaN as an argument

Output:

Explanation of the example:

In the above example, the javascript isInteger method is passed the value This is a string which is a NaN value (i.e. it is not a number). The Number.isInteger('This is a string') would return false, thus the code outputs 'It is not an integer' .

Example 5. Passing Infinity as an Argument

Output:

Explanation of the example:

In the above example, the javascript isInteger method is passed the value 5/0 which is an infinite value. The Number.isInteger(5/0) would return false, thus the code outputs 'It is not an integer .

Browser Compatibility

The javascript isInteger method is compatible in all browsers except Internet Explorer.

Conclusion

  • The javascript isInteger method checks if the value passed to it is an integer or not.
  • The javascript isInteger method returns boolean value.
  • The javascript isInteger method returns false for non numeric values.
  • The javascript isInteger returns true when positive or negative integers are passed as arguments.
  • The javascript isInteger returns true when zero is passed as an argument.
  • The javascript isInteger is a method of Number object in javascript.