It really depends on what you want to achieve. If you want to "emulate" strongly typed languages then I suggest you not trying. As others mentioned all numbers have the same representation (the same type).
Using something like Claudiu provided:
isInteger( 1.0 ) -> true
which looks fine for common sense, but in something like C you would get false
x=1;
x===parseInt(x); // true
x="1";
x===parseInt(x); // false
x=1.1;
x===parseInt(x); // false, obviously
// BUT!
x=1.0;
x===parseInt(x); // true, because 1.0 is NOT a float!
This confuses a lot of people. Whenever something is .0, it's not a float anymore. It's an integer. Or you can just call it "a numeric thing" for there is no strict distinction like back then in C. Good old times.
So basically, all you can do is check for integer accepting the fact that 1.000 is an integer.
Interesting side note
There was a comment about huge numbers. Huge numbers mean NO problem for this approach; whenever parseInt is unable to handle the number (for it's too big) it will return something else than the actual value so the test will return FALSE. This is a good thing because if you consider something a "number" you normally expect JS to be able to calculate with it - so yes, numbers are limited and parseInt will take this into consideration, to put it this way.
Try this:
<script>
var a = 99999999999999999999;
var b = 999999999999999999999; // just one more 9 will kill the show!
var aIsInteger = (a===parseInt(a))?"a is ok":"a fails";
var bIsInteger = (b===parseInt(b))?"b is ok":"b fails";
alert(aIsInteger+"; "+bIsInteger);
</script>
In my browser (IE8) this returns "a is ok; b fails" which is exactly because of the huge number in b. The limit may vary but I guess 20 digits "ought to be enough for anybody", to quote a classical :)
It really depends on what you want to achieve. If you want to "emulate" strongly typed languages then I suggest you not trying. As others mentioned all numbers have the same representation (the same type).
Using something like Claudiu provided:
isInteger( 1.0 )
-> truewhich looks fine for common sense, but in something like C you would get
false
For integers I use this
Condtion for floating validation :
Condtion for Integer validation :
Hope this might be helpful.
It's simple as:
Try this in console:
This confuses a lot of people. Whenever something is .0, it's not a float anymore. It's an integer. Or you can just call it "a numeric thing" for there is no strict distinction like back then in C. Good old times.
So basically, all you can do is check for integer accepting the fact that 1.000 is an integer.
Interesting side note
There was a comment about huge numbers. Huge numbers mean NO problem for this approach; whenever parseInt is unable to handle the number (for it's too big) it will return something else than the actual value so the test will return FALSE. This is a good thing because if you consider something a "number" you normally expect JS to be able to calculate with it - so yes, numbers are limited and parseInt will take this into consideration, to put it this way.
Try this:
In my browser (IE8) this returns "a is ok; b fails" which is exactly because of the huge number in b. The limit may vary but I guess 20 digits "ought to be enough for anybody", to quote a classical :)
THIS IS FINAL CODE FOR CHECK BOTH INT AND FLOAT
OR
Haven't had a case where this didn't do the job.