reduceRight()
The reduceRight()
method in JavaScript executes a reducer function on each element of an array, from right to left, to produce a single accumulated result. It is commonly used when the order of operations matters—such as evaluating expressions, parsing nested structures, or performing right-associative computations like exponentiation.
Syntax
reduceRight(callbackFn, initialValue)
Parameters:
callbackFn
: A function to execute on each element in the array. It takes four arguments:accumulator
: The accumulated result from the previous callback.currentValue
: The current element being processed.currentIndex
: The index of the current element.array
: The arrayreduceRight()
was called upon.
initialValue
(Optional): A value to use as the first argument to the first call ofcallbackFn
. If not provided, the last element in the array is used as the initial value, and iteration starts from the second-to-last element.
Return value:
Returns a single value resulting from the reduction of the array, working from right to left.
reduceRight()
vs reduce()
reduce()
processes array elements from left to right, useful for left-associative operations like summing or accumulating values.reduceRight()
processes elements from right to left, ideal for right-associative logic like parsing or reversing operations.
In the following example, reduce()
combines array elements from left to right, while reduceRight()
combines them from right to left, resulting in reversed concatenation:
const stringArray = ['0', '2', '4', '6', '🐈', '→'];const reduceMethod = stringArray.reduce((prev, cur) => prev + cur);const reduceRightMethod = stringArray.reduceRight((prev, cur) => prev + cur);console.log(`reduceMethod : ${reduceMethod}`);console.log(`reduceRightMethod : ${reduceRightMethod}`);
It generates the following output:
reduceMethod : 0246🐈→reduceRightMethod : →🐈6420
Example 1: Reverse Sum with reduceRight()
The following code shows the reduceRight()
without an initial value, and the order of execution of the callback:
const michiArray = [0, 1, 2, 3, 4];const reducedMichi = michiArray.reduceRight((accumulator, currentValue) => accumulator + currentValue);console.log(reducedMichi);
call # | accumulator | currentValue | index | Return value |
---|---|---|---|---|
First call | 4 | 3 | 3 | 7 |
Second call | 7 | 2 | 2 | 9 |
Third call | 9 | 1 | 1 | 10 |
Fourth call | 10 | 0 | 0 | 10 |
The output of this code is:
10
Example 2: Reverse Name Construction with reduceRight()
The following code shows the reduceRight()
with an initial value, and the order of execution of the callback:
const favoriteName = ['👑', 'y', 'n', 'a', 'i'];const princess = favoriteName.reduceRight((accumulator, currentValue) => accumulator + currentValue,'L');console.log(princess);
call # | accumulator | currentValue | index | Return value |
---|---|---|---|---|
First call | L | i | 4 | Li |
Second call | Li | a | 3 | Lia |
Third call | Lia | n | 2 | Lian |
Fourth call | Lian | y | 1 | Liany |
Fifth call | Liany | 👑 | 0 | Liany👑 |
The output of this code is:
Liany👑
Codebyte Example
Run the following code to understand the working of the reduceRight()
method:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours