javascript reduce method

Javascript Reduce Array Method

By onjsdev

Reduce is one of the array methods in javascript allowing given an array to reduce to a single output. In this article we will discuss its syntax and usage with examples, so let's get started.

Syntax Of Javascript Array Reduce Method

array.reduce((accumulator,current)=>output, firstAccumulatorValue)

The reduce array method takes two parameters, one of which is a callback function and other is the starting value of accumulator variable.

On other hand the callback function also takes two parameters:

  • accumulator => previous output of the callback
  • current => current item in the array

Example Of Reduce Method In JavaScript

Suppose you have an array of numbers, and you want to find their sum using the reduce method:

const numbers = [1, 2, 7];

const sum = numbers.reduce((acc, curr) => {
  console.log(acc, curr);

    0 1
    1 2
    3 7

  return acc + curr;
}, 0);

console.log(sum); // 10

As you see above, the first value of the accumulator variable is 0 and the first current element is 1. The reduce() method iterates through the elements in the array and runs the callback function.

The callback function adds the accumulator and current element, resulting in a new accumulator value. This process continues for each element in the array until all elements have been processed. The final result is the accumulated sum of all the elements in the array.


The reduce() method in JavaScript is a powerful array method that provides a way to reduce an array of values down to a single output value by processing each element in the array with a callback function.

In this article, we have discussed the reduce() method in JavaScript by providing an example of how to sum values in an array, along with its explanation.

Thank you for reading.