The simplest form of isInt is as follows (for a number n):
parseInt(n) === n; //isInt
That's it - you don't need a function call! Similarly, we have the following:
parseInt(n) !== n; //isNotInt
parseFloat(n) === n; //isFloat
parseFloat(n) !== n; //isNotFloat
Comment
Why this:
var n = 3.0;
alert(parseInt(n)===n);
alerts "true"?
Parent comment
The simplest form of isInt is as follows (for a number n): parseInt(n) === n; //isInt That's it - you don't need a function call! Similarly, we have the following: parseInt(n) !== n; //isNotInt parseFloat(n) === n; //isFloat parseFloat(n) !== n; //isNotFloat
Replies
Python does the same:
>>> 3.0 == 3
True
But in python you can do:
>>> 3 is 3
True
>>> 3.0 is 3.0
True
>>> 3.0 is 3
False