- function: a callback function that will be executed once for each array element in ascending index order. The function takes the following parameters:
- total: acts as a return accumulator and matches the previously returned value of the callback function. The first time the callback function is called, the sum equals initialValue if one was provided or the first value in the array otherwise
- currentValue: the value of the current element
- currentIndex (optional): array index of the current element. It starts at index 0 if initialValue is specified, otherwise at index 1
- array (optional): the array to which the current element belongs (which is being traversed)
- initialValue (optional): the value used as the first argument of the first call for the callback function. If no initial value is specified, then the first element of the array will be used.
How to reduce an array from right to left?
You can use the reduceRight() method to call the callback in descending index order (right-to-left).