DEV Community

Cover image for Learn how Array.reduce() works under the hood in JavaScript
Danities Ichaba
Danities Ichaba

Posted on

Learn how Array.reduce() works under the hood in JavaScript

Array.reduce() is a powerful built-in function in JavaScript that is used to reduce an array to a single value by applying a given function to each element of the array. The Array.reduce() function takes two arguments: a callback function and an optional initial value.

In this article, we will explore how the Array.reduce() function works under the hood in JavaScript.

The basics of reduce()
Before we dive into the technical details of how Array.reduce() works, let's start by reviewing the basic syntax and usage of the function. Here's an example of how Array.reduce() can be used to find the sum of all the elements in an array:

const numbers = [1, 2, 3, 4, 5];

const sum = numbers.reduce((acc, current)=> {
  return acc + current;
}, 0);

console.log(sum); // Output: 15

Enter fullscreen mode Exit fullscreen mode

In this example, we start with an array of numbers and use the Array.reduce() function to find the sum of all the elements in the array.

How Array.reduce() works under the hood

Now let's take a closer look at how the Array.reduce() function works behind the scenes.

When you call Array.reduce() on an array, JavaScript starts by setting the initial value of the accumulator to the initial value you provided (if any). If you didn't provide an initial value, JavaScript uses the first element of the array as the initial value and starts iterating from the second element.

For each element in the array,Array.reduce() calls the callback function that you passed as an argument. This callback function takes two arguments:

  1. The accumulator value, which is the value returned from the previous iteration of the callback function (or the initial value, if this is the first iteration)

  2. The current element being processed

The callback function returns a value that becomes the new accumulator value for the next iteration of the callback function.

Once the callback function has been called for every element in the array, Array.reduce() returns the final value of the accumulator.

Here's a simplified version of what happens under the hood when you call Array.reduce():

function reduce(array, callback, initialValue) {
  let accumulator = initialValue 

if(accumulator !== underfined){
return initialValue
}else {
return arrau[0]
}
for (let i = initialValue !== undefined ? 0 : 1; i < array.length; i++) {
    accumulator = callback(accumulator, array[i]);
  }

  return accumulator;
}
Enter fullscreen mode Exit fullscreen mode

This code is a simplified version of what happens when you call Array.reduce(). In reality, the Array.reduce() function is implemented more efficiently and with more error checking, but this code captures the basic idea.

Conclusion
The Array.reduce() function is a powerful tool that makes it easy to reduce an array to a single value by applying a given function to each element of the array. By understanding how Array.reduce() works under the hood, you can better appreciate the elegance and efficiency of this built-in function. Whether you're working on a small JavaScript project or a large-scale web application, Array.reduce() is a valuable tool to have in your arsenal.

Top comments (0)