Member-only story
Learn These Three JavaScript Functions and Become a Reduce Master!

Reducer functions are simple yet challenging to master. The minimalist solutions that reducers implement are often too abstract for new developers. It can also be difficult to visualize the nearly limitless ways that reducers can be used to manage and simplify data. Ultimately, most programmers at some point will find reducers mystifying.
Let’s strengthen our knowledge by writing some JavaScript! We will start by going over the fundamentals of reducers by creating a reduce function. Then we will create advanced functions and break down their execution step by step. Finally, we will use our functions to reduce API results from theMovieDb.org. Let’s dive in!
The Basics
Libraries like Redux use reducers to manage state across large web applications. However, reducer functions can also be used to change the state of an individual data structure.
Let’s check out an example that can be used to reduce an array to an integer:
Our reducer add()
takes two arguments: an accumulator and a current value. The Array.Reduce()
method executes a reducer and sets its return value equal to the accumulator, this continues until the array iteration completes. At invocation, the accumulator is set to the initial value; an optional variable. If no initial value is given then the accumulator is set to the first element of the array.
If this is confusing, don’t worry! We will begin breaking down these steps shortly. Let’s start by creating a function that replicates the behavior of Array.Reduce()
Our reduce
function takes three arguments: an array to reduce, a reducer function to execute, and an initial value for the accumulator. We use .shift()
to set the…