Using
is not the best way to check for being numeric, since it allows trailing garbage in the argument (e.g.
would yield 2.2).
There is also no reason to do a
. You already have a boolean, so a simple negation would suffice (pet peeve, sorry
.
When converting a
string to a number, I recommend using the
function. A (very slightly) more efficient method is to use the prefix plus operator. More on converting to numbers can be found in the
comp.lang.javascript FAQ (it's a great FAQ, read it all!).
So:
That accepts all strings that are number literals in Javascript. Often when someone wants data to be numeric, they have a specific format in mind. If the format is simple, using a regular expression on the string might be more efficient than converting it to a number that isn't used anyway. E.g., to check whether a string is an integer numeral:
Regards
/L