Javascript Array reduceRight()

The JavaScript Array reduceRight() method executes a reducer function on each element of the array and applies it against an accumulator.

The syntax of the reduceRight() method is:

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

Here, arr is an array.


reduceRight() Parameters

The reduceRight() method takes in:

  • 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 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.


Return value from reduceRight()

  • 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: Sum of All Values of Array

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

function sum_reducer(accumulator, currentValue) {
  return accumulator + currentValue;
}

let sum = numbers.reduceRight(sum_reducer);
console.log(sum); // 21

// using arrow function
let summation = numbers.reduceRight(
  (accumulator, currentValue) => accumulator + currentValue
);
console.log(summation); // 21

Output

21
21

Example 2: Subtracting Numbers in Array

const numbers = [50, 300, 20, 100, 1800];

// subtract all numbers from last number
// since 1st element is called as accumulator rather than currentValue
// 1800 - 100 - 20 - 300 - 50 
let difference = numbers.reduceRight(
  (accumulator, currentValue) => accumulator - currentValue
);
console.log(difference); // 1330

const expenses = [1800, 2000, 3000, 5000, 500];
const salary = 15000;

// function that subtracts all array elements from given number
// 15000 - 500 - 5000 - 3000 - 2000 - 1800
let remaining = expenses.reduceRight(
  (accumulator, currentValue) => accumulator - currentValue,
  salary
);
console.log(remaining); // 2700

Output

1330
2700

This example clearly explains the difference between passing an initialValue and not passing an initialValue.


Example 3: Create Composite Functions

// create composite functions
const composite = (...args) => (initialArg) => args.reduceRight((acc, fn) => fn(acc), initialArg);

const sqrt = (value) => Math.sqrt(value);
const double = (value) => 2 * value;

const newFunc = composite(sqrt, double);

// ( 32 * 2 ) ** 0.5
let result = newFunc(32);
console.log(result); // 8

Output

8

We know that function composition is the way in which the result from one function is passed to another function. The execution happens from right to left, so we can take advantage of the reduceRight() function.

In this example, we have created a composite() function that takes in an arbitrary number of arguments. This function returns another function that takes in initialArg and returns this value reduced by applying it against given functions from right to left.


Recommended Reading: JavaScript Array reduce()