I've run across this function at work:
function isInt(value) {
var x;
if (isNaN(value)) {
return false;
}
x = parseFloat(value);
return (x | 0) === x;
}
Two questions about the above code:
What is going on with the return statement? I've never used Bitwise operators in code, yet - I understand that this is the Bitwise OR operator. How does this determine whether the number passed is an integer or not?
Perhaps more pertinently, is this function even necessary? I know the JavaScript Number object has an 'isInteger' method. Would it not be easier to use this?
Thanks for any responses!