Why does Math.min([])
evaluate to 0
?
I would expect that it would evaluate to NaN
since MDN's manpage for Math.min states "If at least one of the arguments cannot be converted to a number, NaN is returned."
So I guess the refined question is why does [] coerce to 0? Especially considering that []
is truthy (i.e. !![] === true
) and Math.min(true) === 1
. I am thinking about this wrong?
Tested on Node v7.0.0
min() The static function Math. min() returns the lowest-valued number passed into it, or NaN if any parameter isn't a number and can't be converted into one.
max() starts with a search value of -Infinity , because any other number is going to be greater than -Infinity. Similarly, Math. min() starts with the search value of Infinity : “If no arguments are given, the result is Infinity .
when you execute Math. min(), you will get (Infinity) and when you execute Math. max(), you will get (-Infinity). by this result for sure if you compare Math.
The min() function returns the smallest value from the numbers provided. If no parameters are provided, the min() function will return Infinity. If any of the parameters provided are not numbers, the min() function will return NaN.
Why does
Math.min([])
evaluate to0
?
Because the spec says so:
Math.min
casts each parameter to a number using...
ToNumber
casts objects to a number using...
ToPrimitive
casts objects to primitive values using...
[[Default Value]]
internal method converts objects to primitives based on their hint parameter.
The default hint for all objects is string. Which means the array gets converted to a string, which for []
is ""
.
ToNumber
then converts ""
to 0
, per the documented algorithm
Math.min
then takes the only parameter and returns it, per its algorithm.
This happens because []
is coerced to 0
.
You can see this with the following call:
(new Number([])).valueOf(); // 0
Therefore, calling Math.min([])
is the same as calling Math.min(0)
which gives 0
.
I believe that the reason that new Number([])
treats []
as 0
is because:
Number(value)
constructor uses a ToNumber
function.ToNumber(value)
function says to use ToPrimitive
for an object
type (which an array is).[]
becomes ""
, [0]
becomes "0"
and [0, 1]
becomes "0,1"
.[]
into ""
which is then parsed as 0
.The above behaviour is the reason that an array with one or two numbers inside it can be passed into Math.min(...)
, but an array of more cannot:
Math.min([])
is equal to Math.min("")
or Math.min(0)
Math.min([1])
is equal to Math.min("1")
or Math.min(1)
Math.min([1, 2])
is equal to Math.min("1,2")
which cannot be converted to a number.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With