Math.min
is capable of accepting multiple arguments, unlike functions such as parseInt
. When using Math.min with reduce, the values like index
and array
are passed to it.
To demonstrate this concept, we can proxy Math.min by following these steps:
First, we create a proxy for Math.min:
var oldMath = Math.min;
Math.min = function (){
console.log(arguments)
return oldMath.apply(Math, arguments);
}
Then we execute the second version:
[-12, 57, 22, 12, -120, -3].reduce(Math.min);
This will output:
[-12, 57, 1, Array[6]]
As the last element in the array is not a number, the result becomes NaN
A similar example from MDN illustrates this point:
["1", "2", "3"].map(parseInt);
While one might expect [1, 2, 3], the actual result would be [1, NaN, NaN]
parseInt
typically only uses one argument, but it actually takes two – the second being the radix. When used with Array.prototype.map, parseInt receives 3 arguments: the element, the index, and the array. While the third argument is disregarded, the second could lead to confusion due to how parseInt function interprets it.