Javatpoint Logo
Javatpoint Logo

JavaScript Array reduceRight() Method

The reduceRight() method reduces the given array elements into a single value by executing a reducer function. The reducer() function is applied against the accumulator and reduces all the elements from right to left.



callback: It is the callback function that executes over each array element. It undertakes the following arguments:

  1. accumulator: It accumulates the initialValue or previously returned values by the callback function.
  2. currentValue: It holds the current array element in process.
  3. currentIndex: It is optional. It holds the current value's index, being in process.
  4. array: It is optional. It is the source array to which the elements belong.

initialValue: It is an optional argument which uses its supplied value as an accumulator to the initial calling of the callback function.


It reduces a single value as an output.

Points to note:

  1. When we invoke the callback function for the first time, the accumulator and currentValue can be one of the two values.
  2. When we provide the initialValue in the function, the accumulator will hold the value of the initialValue, and currentValue will hold the last element of the array.
  3. When no initialValue is supplied, then accumulator will hold the last array element, and currentValue will hold the second-last array element.
  4. When an empty array is present with no initialValue, it will throw an error known as TypeError.
  5. When it's an array of one element with no initialValue or an empty array with an initialValue, that element will be returned without invoking the callback function.

JavaScript Array reduceRight() Method Example

Let's see some examples to understand better:


Here's a simple implementation of the Array reduceRight() method.

Test it Now


JavaScript Array reduceRight() Method

But, it is not clear that reduceRight() method is working from right-to-left or right-to-left in the above example.


Here's an implementation which shows the working direction of the reduceRight() method.

Test it Now


JavaScript Array reduceRight() Method

Thus, it is clear from the above output that reduceRight() works from right-to-left.

Although both reduceRight() and reduce() methods reduce the array elements into a single value, there is a difference between them. The reduce() method reduces the array elements from the left index position to the right. On the other hand, the reduceRight() method reduces the array elements from the right index position towards left.

Let's see the below implementation to understand better.


Test it Now


JavaScript Array reduceRight() Method

We can implement many more examples to understand the working of the method deeply.

Next TopicJavaScript Array

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA