MY code is:
function isNumber(n){
return typeof n == \'number\' && !isNaN(n);
}
window.onload=function(){
var a=0,b=1,c=2.2,d=-3,e=-4.4,f=10/3;
var
If you want to check whether a number is a real number, you should also check whether it's finite:
function isNumber(n){
return typeof n == 'number' && !isNaN(n) && isFinite(n);
}
Another method (explanation below):
function isNumber(n){
return typeof n == 'number' && !isNaN(n - n);
}
Since JavaScript numbers are representing real numbers, the substraction operand on the same number should produce the zero value (additive identity). Numbers out of range should (and will) be invalid, NaN.
1 - 1 = 0 // OK
Infinity - Infinity = NaN // Expected
NaN - NaN = NaN // Expected
NaN - Infinity = NaN