What is reduce right in javascript?

Examples

Subtract the numbers in the array, starting from the end:

const numbers = [175, 50, 25];

document.getElementById("demo").innerHTML = numbers.reduceRight(myFunc);

function myFunc(total, num) {
  return total - num;
}

Try it Yourself »

Subtract the numbers, right-to-left, and display the sum:

const numbers = [2, 45, 30, 100];
document.getElementById("demo").innerHTML = numbers.reduceRight(getSum);

function getSum(total, num) {
  return total - num;
}

Try it Yourself »


Definition and Usage

The reduceRight() method executes a reducer function for each array element.

The reduceRight() method works from right to left.

The reduceRight() method returns a single value: the function's accumulated result.

The reduceRight() method does not execute the function for empty elements.


Syntax

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

Parameters

Parameter Description
function() Required.
A function to be run for each element in the array.
Reducer function parameters:
total Required.
The initialValue, or the previously returned value of the function.
currentValue Required.
The value of the current element.
currentIndex Optional.
The index of the current element.
arr Optional.
The array the element belongs to.
initialValue Optional.
A value to be passed to the function as the initial value

Return Value

The accumulated result from the last call of the callback function.



Browser Support

reduceRight() is an ECMAScript5 (ES5) feature.

ES5 (JavaScript 2009) fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes 9-11 Yes Yes Yes Yes

In this tutorial, you will learn about the JavaScript Array reduceRight() method with the help of examples.

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.


Recommended Reading: JavaScript Array reduce()

What is reduce () in JavaScript?

The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

What is the difference between reduce and reduceRight?

The only difference between reduce and reduceRight is the direction of the iteration. reduce iterates over the array elements left to right. And reduceRight iterates over the elements right to left.

What is reduce used for?

The reduce() method reduces all the elements in an array into a single output value. The output value can be a number, object, or string. reduce() method has two arguments. The first one is a callback function and the second one is the initial value.

Should you use reduce in JavaScript?

The easiest and perhaps most popular use of the JavaScript reduce method is to get the sum total of a list of numbers. On every iteration, we simply added the current teams' points to the previously accumulated points, and then we returned that as the accumulated value for the next iteration.