1

I'm trying to write a helper function that will cast a String coming 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?

connexo
  • 53,704
  • 14
  • 91
  • 128
  • 5
    its part of the standard. read the docs – Daniel A. White Apr 01 '19 at 15:50
  • for validation use regexp `/^-?\d*\.?\d*$/.test(stringWithNumber)` – Kamil Kiełczewski Apr 01 '19 at 15:52
  • This question has been answered on this [link](https://stackoverflow.com/questions/4564158/what-is-the-difference-between-parseintstring-and-numberstring-in-javascript) – Aderbal Farias Apr 01 '19 at 15:53
  • [Number()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) !== [parseFloat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat). *Removed explanation, better in answers below.* – Lewis Apr 01 '19 at 15:55

4 Answers4

3

Number constructor tries to coerce the argument to number. So empty string '', false, null and all falsy values become 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 comments, 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.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

2
  • 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 committee wants it to: http://www.ecma-international.org/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...

georg
  • 211,518
  • 52
  • 313
  • 390
0

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.

Diogenis Siganos
  • 779
  • 7
  • 17
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.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

connexo
  • 53,704
  • 14
  • 91
  • 128