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?