The reduce
method executes the callbackFn
function once for each element present in the typed array, excluding holes in the typed array, receiving four arguments: the initial value (or value from the previous callbackFn
call), the value of the current element, the current index, and the typed array over which iteration is occurring.
The first time the callback is called, accumulator
and currentValue
can be one of two values. If initialValue
is provided in the call to reduce
, then accumulator
will be equal to initialValue
and currentValue
will be equal to the first value in the typed array. If no initialValue
was provided, then accumulator
will be equal to the first value in the typed array and currentValue
will be equal to the second.
If the typed array is empty and no initialValue
was provided, TypeError
would be thrown. If the typed array has only one element (regardless of position) and no initialValue
was provided, or if initialValue
is provided but the typed array is empty, the solo value would be returned without calling callbackFn
.