Why does the second example return NaN, whereas the first one works?
const numbers = [ 1, 2, 3 ]
console.log('passing arrow funciton', numbers.reduce((l, r) => Math.max(l, r)) ) // 3
console.log('passing bound function', numbers.reduce(Math.max.bind(Math)) ) // NaN
To give you some context the reduce function requires a Callback param. Furthermore, the callback requires two params, the Accumulation and Current element ( you can also call them left and right etc. ). There are more parameters but they are optional.
I've tried to mock the reduce function and the example works like a charm
const numbers = [1, 2, 3]
const reduce = (array, func) => {
let largest = 0
array.forEach
(
(number, i, a) =>
a[i + 1]
? largest = func(number, a[i + 1])
: console.log('done')
)
return largest
}
console.log( 'mock reduce', reduce(numbers, Math.max.bind(Math)) ) // 3

Math.max(...numbers)?.reduce()function passes 4 parameters to the callback, and one of those parameters is not a number.NaNwhenMath.max()tries to convert it to a number. The documentation says that it's "optional", but that means that your callback function can ignore it, which is exactly what your first callback function does..reduce()andconsole.log(arguments)in the callback.