Skip to main content

reduce

The reduce() method is used to iterate over the elements of an array and reduce all elements to a single value.

Syntax

// Using Arrow function
reduce((previousValue, currentValue) => {
/* … */
});
reduce((previousValue, currentValue, currentIndex) => {
/* … */
});
reduce((previousValue, currentValue, currentIndex, array) => {
/* … */
});

reduce((previousValue, currentValue) => {
/* … */
}, initialValue);
reduce((previousValue, currentValue, currentIndex) => {
/* … */
}, initialValue);
reduce((previousValue, currentValue, currentIndex, array) => {
/* … */
}, initialValue);

// Using Callback function
reduce(callbackFn);
reduce(callbackFn, initialValue);

// Using Inline callback function
reduce(function (previousValue, currentValue) {
/* … */
});
reduce(function (previousValue, currentValue, currentIndex) {
/* … */
});
reduce(function (previousValue, currentValue, currentIndex, array) {
/* … */
});

reduce(function (previousValue, currentValue) {
/* … */
}, initialValue);
reduce(function (previousValue, currentValue, currentIndex) {
/* … */
}, initialValue);
reduce(function (previousValue, currentValue, currentIndex, array) {
/* … */
}, initialValue);

Parameters

callBack function

The callBack function have following arguments

  • previousValue: If initialValue is declared, the previousValue will have the value of initialValue. If not, previousValue will have the value of array[0] and on each call the value gets updated based on the index.

  • currentValue: The value of currentValue will be array[0] if initialValue is specified. If not, currentValue will be set to the value of array[1] and keep updated on each call.

  • currentIndex: It is the index position of currentValue in the array. On first call, it's value will be 0 if initialValue is specified. If not value is 1.

  • array: The array at which reduce is applied.

  • initialValue: (optional) The initial valueinitialValue is initialized to the previousValue on the first call which cause currentValue to be initialized to the first value in the array. If initialValue is not specified, previousValue will be initialized to first value of the array and current value will be initialized to the second value of the array.

Example

With initialValue

var array = [10, 20, 30, 40, 50];
var result = array.reduce(
(previousValue, currentValue, currentIndex, array) => {
return previousValue + currentValue;
},50
);
console.log(result);
// expected output: 200

In the given example an initialValue is declared with a value of 50. By consoling previousValue and currentValue we will get an idea of which value is reduce method iterating. At last an array is converted to a single value and it is returned.

Without an initialValue

var array = [10, 20, 30, 40, 50];
var result = array.reduce(
(previousValue, currentValue, currentIndex, array) => {
return previousValue + currentValue;
}
);
console.log(result);
// expected output: 150

The reduce method iterates over the array elements by elements, at each step adding currentValue with the result of the previousValue until there are no more elements in the array to add.