Javascript Array reduceRight()

The reduceRight() method reduces the array to a single value by executing a callback function on two values of the array (from right to left).

Example

let numbers = [1, 2, 3, 4];

// function that adds last two values of the numbers array
function sum_reducer(accumulator, currentValue) {
  return accumulator + currentValue;
}

// returns a single value after reducing the numbers array let sum = numbers.reduceRight(sum_reducer);
console.log(sum); // Output: 10

reduceRight() Syntax

The syntax of the reduceRight() method is:

arr.reduceRight(callback(accumulator, currentValue), initialValue)

Here, arr is an array.


reduceRight() Parameters

The reduceRight() method can take two parameters:

  • callback - The function to execute on each array element. It takes in:
    • accumulator - It accumulates the callback's return values. It is initialValue for the first call if supplied.
    • currentValue - The current element being passed from the array.
  • initialValue (optional) - A value that will be passed to callback() on the first call. If not provided, the last element acts as the accumulator on the first call and callback() won't execute on it.

Note: Calling reduceRight() on an empty array without initialValue will throw TypeError.


reduceRight() Return Value

  • Returns the value resulting after reducing the array.

Notes:

  • reduceRight() executes the given function for each value from right to left.
  • reduceRight() does not change the original array.
  • It is almost always safer to provide initialValue.

Example 1: Using reduceRight() Method

let numbers = [1, 2, 3, 4, 5, 6];

// function that adds last two values of the numbers array
function sum_reducer(accumulator, currentValue) {
  return accumulator + currentValue;
}

// returns a single value after reducing the numbers array let sum = numbers.reduceRight(sum_reducer);
console.log(sum);

Output

21

In the above example, we have used the reduceRight() method to convert the numbers array into a single value.

We have defined a function sum_reducer() that takes two arguments and returns its sum.

Since we have not passed initialValue, the last element 6 is accumulator and 5 is currentValue.

The reduceRight() method executes the callback sum_reducer() and adds the resultant sum of 6+5 which is the new accumulator with the new currentValue 4.

This process continues until the first value of numbers is reached.


Example 2: Passing initialValue in reduceRight() Method

let expense = [50, 300, 20, 100, 1800];

// function that returns sum of two values
function add(accumulator, currentValue) {
  return accumulator + currentValue;
}

// adds 0 with last value of expense (i.e 1800) // and executes the callback add() let result = expense.reduceRight(add, 0);
console.log(result);

Output

2270

In the above example, we have passed initialValue- 0 which acts as accumulator.

numbers.reduceRight(add, 0) at first adds 0 with last value of the expense array i.e.1800. Then it executes add() and adds the remaining element of the array(from right to left).

The method finally returns a single value sum i.e. 2270.


Also Read:

Did you find this article helpful?