最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does isNaN(123.) return false? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Jun 10, 2010 at 5:10 vivekramanvivekraman 1951 gold badge3 silver badges8 bronze badges 5
  • 1 I don't understand your question. Since 123 is a number, the predicate "is not a number" is false. What are you expecting? – Gabe Commented Jun 10, 2010 at 5:14
  • 3 In Javascript, 123 == 123. == 123.0 – user42690 Commented Jun 10, 2010 at 5:15
  • 1 123. is not "not a number". NaN is a very specific numerical "value". 123. is just the same as 123.0. – Eric J. Commented Jun 10, 2010 at 5:16
  • yes but is this generally accceptable? – vivekraman Commented Jun 10, 2010 at 5:16
  • 1 123. is a number in any language that supports floating point values, not just javascript. It is fairly standard. – drawnonward Commented Jun 10, 2010 at 5:27
Add a comment  | 

3 Answers 3

Reset to default 18

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.

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.

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.

发布评论

评论列表(0)

  1. 暂无评论