You can check if no value was passed to the function by testing the array of the arguments
passed to the function; so if arguments.length=0
then no arguments were passed to the function and num
will not be defined.
You could also do further checks against num !== undefined && num !== null
(note that non-identity operator !==
is used instead of non-equality operator !=
).
Instead you could just check that if you parse the number it is a valid non-infinite number:
isNumber: function (num) {
if ( arguments.length == 0 )
{
return false;
}
return !isNaN(parseFloat(num))&&isFinite(num);
}
This checks if:
parseFloat(num)
evaluates to a valid number; and if
isFinite(num)
- that valid number is not positive or negative infinity.
If this meets your criteria then you don't need the first check (since parseFloat(undefined)
returns NaN
) and can simplify the function to:
isNumber: function (num) {
return !isNaN(parseFloat(num))&&isFinite(num);
}