When using Number([false])
, it results in NaN
because it converts to Number('false')
. However, ''
(empty or only white-space), null
, and undefined
initialize a Number
as 0
. The Number
wrapper returns a primitive value.
All primitive values have object equivalents except for null
and undefined
, which wrap around the primitive values.
As Touffy pointed out in the comments, the array must be resolved by using toString
on the value before calling the wrapper. Once the string value is determined, the wrapper handles the parsing.
Number([0]) // [0].toString() => Number("0") => 0
Number([undefined]) // [undefined].toString() => Number("") => 0
Number([null]) // [null].toString() => Number("") => 0
Number([false]) // [false].toString() => Number("false") => NaN
Both null
and undefined
are valid arguments for creating a Number
primitive because they represent the absence of a value. Blank or empty strings are also considered empty values. Strings like 'false'
and boolean values like false
are non-empty and not numeric, resulting in NaN
.
Tip: This differs slightly from how integer parsing functions.
parseInt('0') // 0
parseInt('') // NaN
parseInt('false') // NaN
You can explore
string_number_conversions_internal.h
in the Chromium Code Search to understand how parsing works for strings.