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:
- Using
typeof
function isInteger(x)
{
return (typeof x === 'number') && (x % 1 === 0);
}
console.log(isInteger(10));
console.log(isInteger(10.1))
- Using
parseInt
function isInteger(x)
{
return parseInt(x, 10) === x;
}
console.log(isInteger(10));
console.log(isInteger(10.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.