blog thumnail

Making sense of reduce in JavaScript

javascript

Tanveer Sayem / 2022-11-18

2 min read

Making sense of Reduce in JS: Reduce is a higher-order function that is available as an Array method, just like Map or Filter, Let's take an example where we first declare an array that has some number values, and we want to print the sum of all the values in that array.
let numbers = [1,2,3,4,5];
let sum = 0;
for(let i = 0; i < numbers.length; i++) {
	sum += numbers[i];
}
console.log(sum); // 15
To understand reduce we would have to understand what is happening in the above code.
  1. We have an array called numbers
  2. We are declaring a variable called sum where we would want to store the total of all the values from the numbers array
  3. Traditional for loop, iterating through the array and on each iteration adding the number to the sum variable above
  4. Finally, when the for loop is done we are now ready to print the sum variable as it stores the final total
If you click on my affiliates links and shop(anything, not just books), I am going to receive a tiny commission. AND… Most of the time, you will receive an offer. Win/Win! The products that I have are the ones I believe in.
amazon
So far so good. Now think of the sum variable here as the accumulator, meaning it is eventually responsible to store the result of the for loop, and i in the for loop as the current value because i represent the current value like 1,2,3... as it iterates through the array.
Let's rewrite the above code but with reduce.
let numbers = [1,2,3,4,5];
const total = numbers.reduce((accumulator, currentValue) => {
	return accumulator + currentValue;
}, 0)
console.log(total); // 15
So, what is happening in the above code? If you look closely you will see that all we are doing here is calling the sum variable in this case calling it accumulator, inside our callback function in reduce rather than outside, currentvalue is equivalent to i in our previous example, so it iterates through the array numbers and adding it to the value. But what value? We did initiate the sum variable in the previous example with 0. We are doing the same thing here as well, the 0 after the curly braces is the initial value of the accumulator. So, lets write the code more descriptively
let numbers = [1,2,3,4,5];
const total = numbers.reduce((accumulator (or sum), currentValue (or i )) => {
	return accumulator + currentValue;
}, initial value of accumulator)
console.log(total); // 15
We can write less code as well:
let numbers = [1,2,3,4,5];
const total = numbers.reduce((acc, curr) => (acc + curr));
console.log(total) // 15
Thinking about reduce as an alternative to traditional for loop helped me wrap my head around it. Hopefully, it will help you as well.
If you click on my affiliates links and shop(anything, not just books), I am going to receive a tiny commission. AND… Most of the time, you will receive an offer. Win/Win! The products that I have are the ones I believe in.

Subscribe to the newsletter

Get emails from me about web development, tech, and early access to new articles.


  • Home
  • About
  • Newsletter
  • Twitter
  • Github
  • YouTube
  • Setup
  • Guestbook
  • Snippets