Why does the Javascript function call isNaN(123.) return false? (notice the dot (.) after 123). Is this a universally acceptable number or will it cause errors downstream?
I’m validating whether a value is a valid decimal using isNaN along with split. Are there cross-browser issues with isNaN? Should I use a bespoke implementation?
Thanks.
In JavaScript the grammar of a Numeric Literal is expressed like this:
As you can see the
DecimalDigitspart after the dot is optional (opt suffix).I wouldn’t recommend the
isNaNfunction to detect numbers, since type coercion can make some things look strange:isNaNshould be used only to compare againstNaN, since:If you want to detect
Numberobjects,Numbervalues or “parseable” numeric strings, give a look to this function I’ve posted some time ago.