;

How to Check if a Variable is an Integer in JavaScript


Tutorialsrack 27/02/2022 Jquery Javascript

In this article, you’ll learn how to check if a variable is an integer or not in javascript. There are various ways to check if a variable is an integer using Number.isInteger(), strict equality (===) operator and using short-circuiting. 

Here are some examples to check if a variable is an integer or not.

Example 1: Using Number.isInteger() Method

To check if the variable is an integer or not, we used the Number.isInteger() method. The Number.isInteger() method to determine whether the passed value is an integer. This method returns true if the value is an integer, otherwise, it returns false.

let example = 10.1;
console.log(Number.isInteger(example));
//Output ==> false

example = 10;
console.log(Number.isInteger(example));
//Output ==> true

example = Infinity;
console.log(Number.isInteger(example));
//Output ==> false

Non-numeric values will return false, even if the value is an instance of the Number class.

let example = undefined;
console.log(Number.isInteger(example));
//Output ==> false

example = null;
console.log(Number.isInteger(example));
//Output ==> false

example = '50';
console.log(Number.isInteger(example));
//Output ==> false

example = new Number(5);
console.log(Number.isInteger(example));
//Output ==> false

JavaScript can only represent up to 16 decimal places, so Number.isInteger() may return unexpected outcomes in cases where JavaScript doesn't have sufficient numeric precision to represent the output.

let example = 5 + 4e-16;
console.log(Number.isInteger(example));
//Output ==> true

example = 5 + 5e-16;
console.log(Number.isInteger(example));
//Output ==> false

Example 2: Using parseFloat() method

In this example, we used the parseFloat() method to check whether a variable is an integer or not.

function isInteger(value) {
  if (isNaN(value)) {
    return false;
  }
  var x = parseFloat(value);
  return (x | 0) === x;
}

let example = 10.1;
console.log(isInteger(example));
// output ==> false

example = 10;
console.log(isInteger(example));
// output ==> true

example = Infinity;
console.log(isInteger(example));
// output ==> false

example = undefined;
console.log(isInteger(example));
// output ==> false

example = null;
console.log(isInteger(example));
// output ==> false

example = '10';
console.log(isInteger(example));
// output ==> true


example = new Number(10);
console.log(isInteger(example));
// output ==> true

example = 5 + 4e-16;
console.log(isInteger(example));
// output ==> true

example = 5 + 5e-16;
console.log(isInteger(example));
// output ==> false

Example 3: Using Strict Equality(===) Operator

In this example, we used strict equality(===) and parseInt() method to check whether a variable is an integer or not.

function isInteger(value) {
  return (value === parseInt(value, 10))
}

let example = 10.1;
console.log(isInteger(example));
// output ==> false

example = 10;
console.log(isInteger(example));
// output ==> true

example = Infinity;
console.log(isInteger(example));
// output ==> false

example = undefined;
console.log(isInteger(example));
// output ==> false

example = null;
console.log(isInteger(example));
// output ==> false

example = '10';
console.log(isInteger(example));
// output ==> false

example = new Number(10);
console.log(isInteger(example));
// output ==> false

example = 5 + 4e-16;
console.log(isInteger(example));
// output ==> true

example = 5 + 5e-16;
console.log(isInteger(example));
// output ==> false

I hope this article will help you to understand how to check if a variable is an integer or not in javascript.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags