Array.reduce
expects a callback function with the following signature:
function(previousElement, currentElement, index, array)
It also requires an optional initial value.
During the first iteration, if an `initialValue` is provided, then the `previousElement` will be set to this value and the `currentElement` will be the `firstArrayElement.`
If no initial value is given, then the `previousElement` will be the `firstArrayElement` and the `currentElement` will be the `secondArrayElement`.
In subsequent iterations, the `previousElement` will hold the value returned by the previous iteration, while the `currentElement` will take on the next value in the array.
So in your example, initially the variable flat
is an empty array, []
.
The statement return flat.concat(current);
will merge the arrays and return a new merged array. This merged array becomes the updated value of flat
for the next iteration, continuing the process. The value returned by the last iteration is the final result that gets printed to the console.