Click on text below to jump to specific point in the video

In this episode we will learn about yet another higher order function, reduce.
Why should you learn reduce?
What is reduce?Recap - map, filter - they transformed a list into something else. Another example is find (not mentioned before). What to do if you cannot find one that fits? That is where reduce comes in.
Reduce is not specific - it is a multi-tool of list transformations. It can be used to express any list transformation.
Reduce is the super list transformation that you can fall back on if you can't find a prebuilt one.
Start demo
We want to summarize all the amounts.
With a for loop

Using reduce
Just like map or filter, reduce is a function on the array object and takes a callback function. But unlike map and filter, it wants an object. The object can be thought of as a starting point. The sum can be passed as the first argument. The iterated item will be the second argument. This return value will in turn be passed as the sum in the next iteration. Which in turn will do the same operation and so on...
Inspecting the output in the console

Let us compare this with the for loop.
The difference in code size is not as dramatic as before. With arrow functions, we can make the code size even smaller.

The example was extremely basic and doesn't showcase the extreme power of reduce.
So I spend the next episode on reduce as well.
Video outline created using VideoJots