Array reduce in javascript (minimal example)

Do you know for cycle? Yes?! Then you know javascript Array reduce method! Let’s say you have an array with numbers e.g. [1,1,1] and you want sum of them. Then you just loop over the array and in each cycle you add next value to intermediate result (current sum).

And that’s reduce, you just loop over the array and in each iteration you take next value and add/subtract/multiply or whatever you want with intermediate result.

Well and Array already knows how to reduce. It’s its prototype method (array.prototype.reduce)

So now you know how Array reduce function works. It’s only going from a list of values to one value.

You can now go to the Mozilla Docs for Array.prototype.reduce for a more in depth overview of javascript reduce method.

Buy me a coffeeBuy me a coffee