0

I already checked what is the best way to check variable type in javascript question on So but didn't found answer of my question.

In javascript how many ways to find if input type is integer? which one is efficient?

I was looking the way to find the integer in javascript and found number of ways to do this:

  1. Using typeof

function isInteger(x) 
{ 
   return (typeof x === 'number') && (x % 1 === 0); 
   
}
console.log(isInteger(10));
console.log(isInteger(10.1))
  1. Using parseInt

function isInteger(x) 
{ 
   return parseInt(x, 10) === x; 
   
}
console.log(isInteger(10));
console.log(isInteger(10.1));
  1. Using Math.round

function isInteger(x)
{ 
  return Math.round(x) === x; 
  
}
console.log(isInteger(10));
console.log(isInteger(10.1));

Is there any other way to find the type of Integer and which one will be most efficient for consider small to large integer values.

NullPointer
  • 7,094
  • 5
  • 27
  • 41

4 Answers4

3

The most intuitive one is Number.isInteger(), at least in my opinion

function isInteger(x)
{ 
  return Number.isInteger(x); 
  
}
console.log(isInteger(10)); // Output: True
console.log(isInteger(10.1)); // Output: False

Edit

As for efficiency, I created a benchmark on jsben.ch where I try all your methods and mine, you can see for yourself ;)

Link

Ahmed Bajra
  • 391
  • 1
  • 2
  • 14
  • 1
    why any specific reason?Is it suitable for long Integer values? – NullPointer Jul 17 '18 at 15:08
  • 1
    I'm afraid this is unsupported on IE however (see it [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger) ) – Romain Valeri Jul 17 '18 at 15:09
  • Too bad it isn't supported in IE. I think it's the most intuitive because it explicitly says what it's doing instead of hiding behind some mathematical or programmatical hack. – Ahmed Bajra Jul 17 '18 at 15:10
  • in your opinion Number.isInteger() is best however your benchmark says typeof.i got confuse – NullPointer Jul 17 '18 at 15:42
  • @NullPointer Hmm, I ran it a couple of times and it seems to be quite inconsistent. – Ahmed Bajra Jul 17 '18 at 15:54
2

I just tested the speed of each code at JSBEN.CH

typeof => 347 ms

parseInt => 338 ms

Math.round => 367 ms

Interestingly, parseInt is the fastest way!

It's only the speed comparison. Go with Number.isInteger(x) instead! It's the instinctive & fastest of all.

Axel
  • 4,365
  • 11
  • 63
  • 122
  • by pointy in comments>parseInt() is an awkward way of doing it because the first thing it does is convert the number to a string – NullPointer Jul 17 '18 at 15:31
  • Then again, use Number.isInteger only IF you don't care about IE compatibility (unsupported there). Besides, thanks a lot for the bench. – Romain Valeri Jul 17 '18 at 16:04
  • parseInt also works for string so Number.isInteger should be used and adding a polyfill for IE would be the right solution. – tarun jain Jul 28 '20 at 04:50
1

Your propositions seem fine, but let's add for completion the polyfill Mozilla puts forward :

Number.isInteger = Number.isInteger || function(value) {
    return typeof value === 'number' && 
    isFinite(value) && 
    Math.floor(value) === value;
};
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
1

I am concluding answer for this question after some resarch

From ECMAscript 6 which introduces a new Number.isInteger() function for precisely this purpose.

However, prior to ECMAScript 6, this is a bit more complicated, since no equivalent of the Number.isInteger() method is provided.

The simplest and cleanest pre-ECMAScript-6 solution (which is also sufficiently robust to return false even if a non-numeric value such as a string or null is passed to the function) would be the following use of the bitwise XOR operator:

function isInteger(x) 
{ 
  return (x ^ 0) === x; 
} 

Others like Math.round() ,typeof and parseInt can be used also to find the Integer.

While this parseInt-based approach will work well for many values of x, once x becomes quite large, it will fail to work properly. The problem is that parseInt() coerces its first parameter to a string before parsing digits. Therefore, once the number becomes sufficiently large, its string representation will be presented in exponential form (e.g., 1e+21). Accordingly, parseInt() will then try to parse 1e+21, but will stop parsing when it reaches the e character and will therefore return a value of 1.

NullPointer
  • 7,094
  • 5
  • 27
  • 41