javascript - Why does Number(null) return 0, and parseFloat(null) return NaN? - Stack Overflow

I'm trying to write a helper function that will cast a String ing from an <input type="tex

I'm trying to write a helper function that will cast a String ing from an <input type="text" /> to a Number.

As I wasn't sure whether to use parseFloat(str) or Number(str) I doublechecked how they handle potentially problematic arguments.

See:

console.log(Number(null)); // 0
console.log(parseFloat(null)); // NaN
console.log(parseInt(null)); // NaN
console.log(isNaN(null)); // false

I'm trying to write a helper function that will cast a String ing from an <input type="text" /> to a Number.

As I wasn't sure whether to use parseFloat(str) or Number(str) I doublechecked how they handle potentially problematic arguments.

See:

console.log(Number(null)); // 0
console.log(parseFloat(null)); // NaN
console.log(parseInt(null)); // NaN
console.log(isNaN(null)); // false

Both parseFloat and parseInt return NaN, whereas Number returns 0. Number seems more coherent here with isNaN(null).

Why is that?

Share Improve this question edited Apr 1, 2019 at 15:57 connexo asked Apr 1, 2019 at 15:48 connexoconnexo 56.9k15 gold badges111 silver badges146 bronze badges 4
  • 5 its part of the standard. read the docs – Daniel A. White Commented Apr 1, 2019 at 15:50
  • for validation use regexp /^-?\d*\.?\d*$/.test(stringWithNumber) – Kamil Kiełczewski Commented Apr 1, 2019 at 15:52
  • This question has been answered on this link – Aderbal Farias Commented Apr 1, 2019 at 15:53
  • Number() !== parseFloat(). Removed explanation, better in answers below. – Someone Commented Apr 1, 2019 at 15:55
Add a ment  | 

4 Answers 4

Reset to default 3

Number constructor tries to coerce the argument to number. So empty string '', false, null and all falsy values bee 0.

Similarly, Number(true) will return 1. Number('some string') will be NaN as 'some string' cannot be converted to a number.

Note that as pointed out in the ments, Number(undefined) is NaN and not 0 in arithmetic operations. (Read here https://codeburst.io/understanding-null-undefined-and-nan-b603cb74b44c)

Read more here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

  • parseInt/Float convert their argument to a string, read it char by char from the left and try to make a number from what they've found. Since String(null) is "null" and a decimal number cannot start with "n", parseInt(null) will be NaN. If you provide a different base, where n, u and l are digits, the result will be different:

console.log(parseInt(null, 32))

  • Number converts its argument as a whole into a number. Number(null) returns +0 because the ECMA mittee wants it to: http://www.ecma-international/ecma-262/7.0/#sec-tonumber . This is probably for historical reasons.

  • global isNaN (not to confuse with Number.isNaN) applies Number to its argument and returns true if the result is NaN. Since Number(null) is +0, isNaN(null) is false.

Hope this sheds some light...

NaN stands for "Not a Number". ParseFloat and ParseInt return real numbers and integers, so this is like an error returned by the function. Number(), on the other hand, represents the object's value. For instance, Number(false) will output 0.

The reason seems to be quite subtle with how parseInt and parseFloat work:

If the argument passed to parseInt/parseFloat is not a String (which null isn't), then it calls toString(null) which returns "[object Undefined]".

If the first character cannot be converted to a number, parseInt returns NaN. https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744835592a4596242.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信