originalArray - the whole array on which reduce is being called.index - the index of the current element of the array.element - the current element of the array.accumulator - the output of previous iteration (in the first iteration it takes the default value, or if not provided, the first element of the array).The reducer function can accept up to 4 arguments:
reduce((accumulator, el) => accumulator * el, 1) A simple example is calculating a product of all elements of the array: // returns 2 * 4 * 6 * 8 = 384 reduce iterates over the collection, calling the reducer function for every element and passing the output of reducer to the next iteration (with one exception mentioned later). It usually accepts 2 arguments: a reducer function and an optional initial value. Reduce is a function that works on collections. I'll use JS definition of reduce as a reference and I'll show what other languages do better in implementing this function. In this post I'll talk about what makes it both so flexible and unintuitive, and I'll present how other iteration functions like map or filter can be implemented on top of reduce. Reduce (aka fold aka inject aka lfold) is a very powerful, flexible, and at the same time an unintuitive and controversial function.