views:

252

answers:

4

Hi,

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.

A: 

The function is called "is Not a Number". So, this should return false in case you enter a number and true if it isn't. Even with the dot behind it this number is still valid. 123. just validates as 123 or 123.0.

Piro
Actually, it's called "is NaN". The difference is subtle, but important.
Ignacio Vazquez-Abrams
+10  A: 

In JavaScript the grammar of a Numeric Literal is expressed like this:

DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt 

As you can see the DecimalDigits part after the dot is optional (opt suffix).

var n = 123.;
typeof n; // "number"

I wouldn't recommend the isNaN function to detect numbers, since type coercion can make some things look strange:

isNaN(""); // false, a empty string coerces to zero
isNaN("\n\t"); // false, a white-space string coerces to zero
isNaN(true); // false, boolean true coerces to 1
isNaN(false); // false, boolean false coerces to zero
isNaN(new Date); // false, Date objects coerce to its numeric timestamp
// etc...

isNaN should be used only to compare against NaN, since:

NaN == NaN; // false!
IsNaN(NaN); // true

If you want to detect Number objects, Number values or "parseable" numeric strings, give a look to this function I've posted some time ago.

CMS
+1 For quoting the spec.
Andy West
thanks. that answers it
vivekraman
A: 

That's very strange. I agree with you, "isNan(123.)" should return true. (true = not a number).

I know that "isNan(123.10)" returns false (false = is a number), in that a decimal is also a number. So maybe the JS function is treating 123. as 123.00? It all depends on what the JS function isNan treats as a "number".

Its compatabile in all browsers - if you check W3.

How about you create your own decimal parser, like this guy has done.

RPM1984
thanks for the link
vivekraman
+1  A: 

NaN or (not a number) is a particular floating-point value, meaning a value that cannot be represented by computers (using the IEEE 754 floating point standard).

Crucially for you, the isNaN() function makes a best-effort to translate numbers to floating-point, which is why your example returns true. See, for example:

isNaN(100);                   //Returns false
isNaN("100");                 //Returns false
isNaN("ABC");                 //Returns true
isNaN("10C");                 //Returns true
isNaN(Math.sqrt(-1));         //Returns true

If you want to access a number such as "123." as a float, try using parseFloat():

parseFloat("123.");           //Returns 123

Furthermore, 123. is a valid number in javascript, which doesn't require a fractional part after the decimal point. So isNan() is behaving correctly, and should return false in this case.

fmark