JavaScript reduceRight method
last modified April 4, 2025
In this article we show how to reduce arrays from right to left using the
reduceRight
method in JavaScript.
Array reduction from right
The reduceRight
method executes a reducer function on each array
element from right to left, resulting in a single output value. It works
similarly to reduce
, but processes the array in the opposite
direction.
This method is useful when the order of operations matters, such as when
processing mathematical expressions or nested function calls. The
reduceRight
method maintains an accumulator that's updated with
each iteration.
The reducer function takes four arguments: accumulator, current value,
current index, and the source array. The accumulator accumulates the callback's
return values. An initial value can be provided as the second argument to
reduceRight
.
Basic reduceRight example
The following example demonstrates the basic usage of the reduceRight
method to concatenate strings from right to left.
const words = ['world', ' ', 'hello']; const sentence = words.reduceRight((acc, word) => acc + word); console.log(sentence);
We create an array of words and use reduceRight
to concatenate them
from right to left. The accumulator starts with the last element ('hello') and
builds the string by prepending each subsequent element.
$ node main.js hello world
Calculating factorial with reduceRight
The reduceRight
method can be used for mathematical operations where
order matters.
const numbers = [1, 2, 3, 4]; const factorial = numbers.reduceRight((acc, num) => acc * num); console.log(factorial);
We calculate the factorial of 4 by multiplying numbers from right to left.
The operation is equivalent to 4 * 3 * 2 * 1. Using reduceRight
ensures the multiplication happens in the correct order.
$ node main.js 24
Flattening nested arrays from right
The reduceRight
method can flatten nested arrays starting from
the rightmost element.
const nested = [[0, 1], [2, 3], [4, 5]]; const flattened = nested.reduceRight((acc, val) => acc.concat(val), []); console.log(flattened);
We flatten a nested array starting from the rightmost sub-array. The empty array
[]
is provided as the initial value. Each sub-array is concatenated
to the accumulator from right to left.
$ node main.js [4, 5, 2, 3, 0, 1]
Composing functions with reduceRight
The reduceRight
method is perfect for function composition where
functions need to be applied from right to left.
const add5 = x => x + 5; const multiply3 = x => x * 3; const subtract2 = x => x - 2; const operations = [add5, multiply3, subtract2]; const composed = operations.reduceRight((acc, fn) => fn(acc), 4); console.log(composed);
We compose three functions that operate on a number. The functions are applied from right to left to the initial value 4. The order is: subtract2, then multiply3, then add5.
$ node main.js 17
Reversing an array with reduceRight
The reduceRight
method can be used to create a reversed version of
an array.
const letters = ['a', 'b', 'c', 'd']; const reversed = letters.reduceRight((acc, letter) => [...acc, letter], []); console.log(reversed);
We create a reversed array by accumulating elements from right to left. The
spread operator ...
is used to create a new array with each
iteration. The empty array []
serves as the initial accumulator.
$ node main.js ['d', 'c', 'b', 'a']
Source
Array reduceRight - language reference
In this article we have demonstrated how to use the reduceRight() method to process arrays from right to left in JavaScript.