In java script all the numbers are internally 64 bit floating point, same as double in java.
There are no diffrent types in javascript, all are represented by type number. Hence you wil l not be able make a instanceof check. However u can use the above solutions given to find out if it is a fractional number. designers of java script felt with a single type they can avoid numerous type cast errors.
Here are efficient functions that check if the value is a number or can be safely converted to a number:
function isNumber(value) {
if ((undefined === value) || (null === value)) {
return false;
}
if (typeof value == 'number') {
return true;
}
return !isNaN(value - 0);
}
And for integers (would return false if the value is a float):
function isInteger(value) {
if ((undefined === value) || (null === value)) {
return false;
}
return value % 1 == 0;
}
The efficiency here is that parseInt (or parseNumber) are avoided when the value already is a number. Both parsing functions always convert to string first and then attempt to parse that string, which would be a waste if the value already is a number.
Thank you to the other posts here for providing further ideas for optimization!
YourJS provides the following two functions which work for all numbers including returning false for -Infinity and Infinity:
function isFloat(x) {
return typeOf(x, 'Number') && !!(x % 1);
}
function isInt(x) {
return typeOf(x, 'Number') && x % 1 == 0;
}
Due to the fact that typeOf() is a YourJS internal function, if you wanted to use these definitions you can download the version for just these functions here: http://yourjs.com/snippets/build/34
In java script all the numbers are
internally 64 bit floating point
, same as double in java. There are no diffrent types in javascript, all are represented by typenumber
. Hence you wil l not be able make ainstanceof
check. However u can use the above solutions given to find out if it is a fractional number. designers of java script felt with a single type they can avoid numerous type cast errors.Why not something like this:
Here are efficient functions that check if the value is a number or can be safely converted to a number:
And for integers (would return false if the value is a float):
The efficiency here is that parseInt (or parseNumber) are avoided when the value already is a number. Both parsing functions always convert to string first and then attempt to parse that string, which would be a waste if the value already is a number.
Thank you to the other posts here for providing further ideas for optimization!
For those curious, using Benchmark.js I tested the most up-voted answers (and the one posted today) on this post, here are my results:
YourJS provides the following two functions which work for all numbers including returning
false
for-Infinity
andInfinity
:Due to the fact that
typeOf()
is a YourJS internal function, if you wanted to use these definitions you can download the version for just these functions here: http://yourjs.com/snippets/build/34