Why does Math.min([]) evaluate to 0? [duplicate]

Why does Math.min([]) evaluate to 0?

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)