.reduce() array method combines each element of an array, using a reducer function you specify, and returns a single value.
The reducer function takes four arguments:
accumulatoris the returned value from the function.
currentValueis the element being iterated over.
index(optional) is the index of the
array(optional) is the array the
.reduce()was called on.
accumulator‘s value accumulates with each iteration through the array, resulting in a single value.
.reduce() takes two arguments:
- The first, is the reducer function that performs the reduction operation.
- The second (optional) argument is an initial value to pass to the function.
Add strings in an array.
Add the values of each element together with an initial value.
Subtract numbers in an array with initial value provided: