JavaScript - Test for an integer

Asked
Active3 hr before
Viewed126 times

7 Answers

javascript
90%

The Number.isInteger() method in JavaScript is used to check whether the value passed to it is an integer or not. It returns true if the passed value is an integer, otherwise, it returns false.,Return Value: The number.isInteger() method returns a boolean value ,i.e. either true or false. It will return true if the passed value is of the type Number and an integer, else it returns false.,How to check the user is using Internet Explorer in JavaScript?,The below examples illustrates the Number.isInteger() method in JavaScript:

load more v
88%

In it Number.isInteger() method returns true if the argument is an integer, otherwise returns false.,Important Note: The method will also return true for floating point numbers that can be represented as integer. Eg: 5.0 (as it is exactly equal to 5 ),It will return true even if the value is a string containing an integer value. So, the results will be:,a === (a|0) → if the given function's argument is exactly the same (===) as the bitwise-transformed value, it means that the argument is an integer.

This will do:

function isInt(value) {
   return !isNaN(value) &&
      parseInt(Number(value)) == value &&
      !isNaN(parseInt(value, 10));
}

Simple parse and check

function isInt(value) {
   var x = parseFloat(value);
   return !isNaN(value) && (x | 0) === x;
}

Short-circuiting, and saving a parse operation:

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

Or perhaps both in one shot:

function isInt(value) {
   return !isNaN(value) && (function(x) {
      return (x | 0) === x;
   })(parseFloat(value))
}

Tests:

isInt(42) // true
isInt("42") // true
isInt(4e2) // true
isInt("4e2") // true
isInt(" 1 ") // true
isInt("") // false
isInt("  ") // false
isInt(42.1) // false
isInt("1a") // false
isInt("4e2a") // false
isInt(null) // false
isInt(undefined) // false
isInt(NaN) // false

Testing reveals that the short-circuiting solution has the best performance (ops/sec).

// Short-circuiting, and saving a parse operation
function isInt(value) {
   var x;
   if (isNaN(value)) {
      return false;
   }
   x = parseFloat(value);
   return (x | 0) === x;
}

If you fancy a shorter, obtuse form of short circuiting:

function isInt(value) {
   var x;
   return isNaN(value) ? !1 : (x = parseFloat(value), (0 | x) === x);
}
load more v
72%

The Number.isInteger() method determines whether the passed value is an integer. ,The value to be tested for being an integer.,A Boolean indicating whether or not the given value is an integer., If the target value is an integer, return true, otherwise return false. If the value is NaN or Infinity, return false. The method will also return true for floating point numbers that can be represented as integer.

Number.isInteger(value)
load more v
65%

// Another option is typeof which return a string
if (typeof(val) === 'number') {
   // Guess what, it's a bloody number!
}
load more v
75%

The typeof operator is used to check the data type of the passed value.,The isNaN() method checks if the passed value is a number.,The Number.isInteger() method is used to check if the number is an integer value.,In the above example, the regex pattern is used to check if the passed argument is an integer value or float value.

Example 1: Using Number.isInteger()

// program to check if a number is a float or integer value

function checkNumber(x) {

   // check if the passed value is a number
   if (typeof x == 'number' && !isNaN(x)) {

      // check if it is integer
      if (Number.isInteger(x)) {
         console.log(`${x} is integer.`);
      } else {
         console.log(`${x} is a float value.`);
      }

   } else {
      console.log(`${x} is not a number`);
   }
}

checkNumber('hello');
checkNumber(44);
checkNumber(3.4);
checkNumber(-3.4);
checkNumber(NaN);
load more v
40%

One can use the remainder operator (%) to express the fact that a number is an integer if the remainder of dividing it by 1 is 0.,However, we are checking for zero, so that’s not an issue here. One problem remains: this function can return true for non-numbers, because % coerces its operands to numbers:,A number is an integer if it remains the same after being rounded to the “closest” integer. Implemented as a check in JavaScript, via Math.round():,You have to be careful with the remainder operator, because the first operand determines the sign of the result: if it is positive, the result is positive, if it is negative, the result is negative.

One can use the remainder operator (%) to express the fact that a number is an integer if the remainder of dividing it by 1 is 0.

function isInteger(x) {
   return x % 1 === 0;
}
load more v
22%

Write a JavaScript function to check whether a value is an integer or not.,Previous: Write a JavaScript function to round a number to a given decimal places. Next: Write a JavaScript function to check whether a variable is numeric or not.,Pictorial Presentation:,Scala Programming Exercises, Practice, Solution

HTML Code:

<!DOCTYPE html>
<html>

<head>
   <meta charset="utf-8">
   <title>Check whether a value is an integer or not</title>
</head>

<body>
</body>

</html>

JavaScript Code:

function is_Int(num) {

   if (typeof num !== 'number')
      return false;

   return !isNaN(num) &&
      parseInt(Number(num)) == num &&
      !isNaN(parseInt(num, 10));
}
console.log(is_Int(23));
console.log(is_Int(4e2));
console.log(is_Int(NaN));
console.log(is_Int(23.75));
console.log(is_Int(-23));

Sample Output:

true
true
false
false
true
load more v

Other "javascript-undefined" queries related to "JavaScript - Test for an integer"