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

javascript - Why parseInt(Infinity) returns NaN and parseFloat(Infinity) returns infinity? - Stack Overflow

programmeradmin1浏览0评论

What could be the reason to make these two functions behave differently for values infinity and -infinity. Does anyone ever find this inconsistency useful?

parseInt(Infinity); // NaN
parseFloat(Infinity); // Infinity

What could be the reason to make these two functions behave differently for values infinity and -infinity. Does anyone ever find this inconsistency useful?

parseInt(Infinity); // NaN
parseFloat(Infinity); // Infinity
Share Improve this question edited Dec 5, 2017 at 4:06 asked Jul 13, 2016 at 16:35 user5818995user5818995 1
  • peterkroener.de/javascript-parseint-vs-parsefloat – soyuka Commented Jul 13, 2016 at 16:41
Add a ment  | 

1 Answer 1

Reset to default 11

The answer to that question is right in the specs for the two functions:

parseInt takes a string parameter.

If the first character cannot be converted to a number, parseInt returns NaN.

parseFloat

parseFloat can also parse and return the value Infinity. You can use the isFinite function to determine if the result is a finite number (not Infinity, -Infinity, or NaN).

parseInt can't return infinity because infinity is not within JavaScript's integer range. whereas it is a valid within the floating point range.

As for useful? I can't say. In the domain that I work in, NaN means an error has happened and I don't believe I have ever used infinity

发布评论

评论列表(0)

  1. 暂无评论