From the course: Learning Functional Programming with JavaScript (ES5)

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Reducing

Reducing - JavaScript Tutorial

From the course: Learning Functional Programming with JavaScript (ES5)

Start my 1-month free trial

Reducing

- [Instructor] There's one more important function that we're going to go over called Reduce. As its name suggests, Reduce takes an array and based on the function we give it, reduces the array down to a single value. For example it might take an array of numbers and reduce it down to a sum or an average. The syntax of Reduce is a little different from the ones we've seen before. And the other functions we've seen, the function we pass in generally needs only one argument representing each element of the array. With Reduce however, the function we pass in needs two arguments. The first argument represents the value accumulated so far, and the second represents the current element. Each time the function is called, its return value becomes the first argument for the next time it's called. So if we wanted to find the sum of all the numbers in an array, it would look like this. Note that we can also pass a third argument into Reduce. This value then becomes starting value for our…

Contents