Am I the only one who read this and realized that none of the answers are addressed to the "integer" part of the question?
This problem
var myInteger = 6; var myFloat = 6.2; if( myInteger > 0 ) // Cool, we correctly identified this as a positive integer if( myFloat > 0 ) // Oh, no! That not an integer!
Decision
To ensure that you are dealing with an integer, you want to bring your value to an integer, and then compare it with yourself.
if( parseInt( myInteger ) == myInteger && myInteger > 0 )
Some neat optimizations
As a bonus, there are several shortcuts for converting from a floating point number to an integer in JavaScript . In JavaScript, all bitwise operators ( |
, ^
, &
etc.) will convert your number to an integer before starting work. I guess this is because 99% of developers do not know the IEEE floating point standard and will be terribly confused when "200 | 2" evaluates to 400 (ish). These shortcuts tend to be faster than Math.floor
or parseInt
, and take up less bytes if you are trying to extract the smallest code possible:
if( myInteger | 0 == myInteger && myInteger > 0 ) // Woot! if( myFloat | 0 == myFloat && myFloat > 0 ) // Woot, again!
But wait, there is more!
These bitwise operators work with 32-bit signed integers. This means that the most significant bit is a sign bit. If you set the sign bit to zero, your number will remain unchanged only if it is positive . You can use this to test positivity and integerity in one hit:
// Where 2147483647 = 01111111111111111111111111111111 in binary if( (myInteger & 2147483647) == myInteger ) // myInteger is BOTH positive and an integer if( (myFloat & 2147483647) == myFloat ) // Won't happen * note bit AND operation is wrapped with parenthesis to make it work in chrome (console)
If you have trouble remembering this confusing number, you can also calculate it in advance:
var specialNumber = ~(1 << 31);
Negative Check
According to a comment by @Reinsbrain, a similar bitwise hack can be used to check for a negative integer. In a negative number, we want the left bit to be 1, therefore, forcing this bit to 1, then the number will only remain unchanged if it was negative, to start with:
// Where -2147483648 = 10000000000000000000000000000000 in binary if( (myInteger | -2147483648) == myInteger ) // myInteger is BOTH negative and an integer if( (myFloat | -2147483648) == myFloat ) // Won't happen
This special number is even easier to calculate:
var specialNumber = 1 << 31;
Edge cases
As mentioned earlier, since JavaScript bitwise operators are converted to 32-bit integers, numbers that do not fit into 32-bit (more than ~ 2 billion) will fail
You can return to a longer solution for these:
if( parseInt(123456789000) == 123456789000 && 123456789000 > 0 )
However, even this solution fails at some point, because parseInt
limited in its accuracy for large numbers. Try the following and see what happens:
parseInt(123123123123123123123);
On my computer in the Chrome console, this outputs: 123123123123123130000
The reason for this is that parseInt treats the input as a 64-bit floating-point IEEE value. This provides only 52 bits for the mantissa, which means a maximum value of ~ 4.5e15 before rounding begins