The Wayback Machine - https://web.archive.org/web/20241212095704/https://www.geeksforgeeks.org/javascript-array-reduce-method/
Open In App

JavaScript Array reduce() Method

Last Updated : 19 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The JavaScript Array.reduce() method iterates over an array, applying a reducer function to each element, accumulating a single output value. It takes an initial value and processes elements from left to right, reducing the array to a single result. It is useful for doing operations like max in an array, min in an array and sum of array

Example to do sum of an array using reduce

JavaScript
const a = [2, 4, 6];

// Use reduce to calculate the sum
const sum = a.reduce((acc, x) => acc + x, 0);

console.log(sum); 

Output
12

We use a lambda expression to provide a function to reduce. We can provide our own function. Below is an example to do sum with out own function.

JavaScript
const a = [2, 4, 6];

function sum2(acc, x) {
  return acc + x;
}

const sum = a.reduce(sum2, 0);

console.log(sum);

Output
12

Syntax of reduce():

array.reduce( function(total, currentValue, currentIndex, arr), initialValue )

Return value:

The JavaScript array reduce method returns a single value/element after traversing the complete array.

Parameters:

This method accepts five parameters as mentioned above and described below:

  • function(total, currentValue, index, arr): It is the required parameter and is used to run for each element of the array. It contains four parameters which are listed below:
  • initialValue: It is an optional parameter and is used to specify the value to be passed to the function as the initial value.
Parameter NameDescriptionRequired/Optional
totalSpecifies the initial value or previously returned value of the functionRequired
currentValueSpecifies the value of the current elementRequired
currentIndexSpecifies the array index of the current elementOptional
arrSpecifies the array object the current element belongs toOptional

Sum of lengths of all Strings using reduce()

JavaScript
const a = ["js", "html", "css"];

// Use reduce to calculate the sum of the lengths of the strings
const res = a.reduce((acc, str) => acc + str.length, 0);

console.log(res);

Output
9

We have a complete list of JavaScript Array methods, to check those please go through this Javascript Array Complete reference article.

JavaScript Array reduce() method- FAQs

How does the reduce() method work with an initial value?

When provided, the initial value is used as the first argument to the first call of the reducer function. If not provided, the first element of the array is used, and the iteration starts from the second element.

Can reduce() be used to flatten an array?

Yes, reduce() can be used to flatten an array by concatenating nested arrays into a single array using the accumulator.

How can I use reduce() to sum all numbers in an array?

To sum numbers, use reduce((accumulator, current) => accumulator + current, 0), where 0 is the initial value.

Is reduce() the same as forEach()?

No, reduce() accumulates values to produce a single result, while forEach() simply iterates over array elements without returning a value.

Can reduce() be used to group array elements?

Yes, reduce() can group array elements into categories by accumulating elements into an object based on a specified condition.


Next Article

Similar Reads

three90RightbarBannerImg