From the course: Learning Functional Programming with JavaScript ES6+

Unlock the full course today

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

Reducing

Reducing - JavaScript Tutorial

From the course: Learning Functional Programming with JavaScript ES6+

Start my 1-month free trial

Reducing

- [Narrator] The final built-in array function that we're going to discuss is called reduce. The purpose of this function might not be quite as apparent as map, filtered or sort. But reduce is definitely just as useful as, if not more useful than the other array functions. What reduce allows us to do is take an array of data, and reduce it down to a single value. So for example, we could take an array of numbers and reduce it down to a sum or an average. Essentially, what reduce does is starts with an initial value, say zero, and for each element in the array, it modifies its initial value in some way until after we've processed all our elements. We end up with the result. In the case of finding the sum of an array of numbers for example, we add each element to the initial value over and over again until we find our final result. And if we were trying to find the product of an array of numbers, we'd multiply each element by the initial value, and so on. So what's the syntax of reduce?…

Contents