Array.prototype.reduceRight()

Folds an array into a single value, visiting elements from the last index to the first.

Signature
reduceRight(callbackFn)
reduceRight(callbackFn, initialValue)
  • Added in JavaScript ES5 (2009)

Parameters

ParameterTypeDescription
$callbackFnFunctionThe reducer, called with (accumulator, currentValue, currentIndex, array). On the first call currentIndex is array.length - 1 when initialValue is given, otherwise array.length - 2.
$initialValueoptionalany= array[array.length - 1]Starting value for the accumulator. If omitted, the last element of the array is used as the accumulator and is skipped as a current value.

Return value

anyThe single value produced by running the reducer from the highest index down to index 0.

Errors

TypeError if the array contains no elements and no initialValue was supplied.

reduceRight() is reduce() walking the other way: it starts at the last index and works back to index 0. Everything else — the accumulator threading, the four callback arguments, the TypeError on an empty array with no seed — behaves identically.

When to reach for it

Direction only matters when your operation is not associative. Summing numbers gives the same answer either way, so use reduce(). Reach for reduceRight() when the order is the point:

  • Function composition. compose(f, g, h) must apply h first, which is exactly a right fold.
  • String and array building where each step wraps the previous result, such as nesting decorators or building a path from the tail.
  • Right-associative parsing, like folding an exponent chain or a chain of ternaries.

What trips people up

People reach for it thinking it reverses the result. It does not — it reverses the visit order. [[0,1],[2,3],[4,5]].reduceRight((a, b) => a.concat(b)) produces [4,5,2,3,0,1] because the accumulator started as the last sub-array. If you want reversed data rather than reversed traversal, use toReversed() and a plain reduce().

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Implement compose(), applying the rightmost function first
  • Nest wrapper functions or middleware so the last one runs innermost
  • Build a string from the tail of a path or a token list
  • Fold a right-associative expression such as an exponent chain

Frequently asked questions

When should I use reduceRight() instead of reduce()?

Only when the operation is not associative and the order of combination changes the answer — function composition, nesting wrappers, or right-associative expressions. For sums, products and counts the two give identical results, so use reduce().

Is reduceRight() the same as reverse().reduce()?

It produces the same value. Both pass (accumulator, element) in that order, so even a non-commutative callback agrees: [1,2,3].reduceRight((a,b) => a - b) and [1,2,3].toReversed().reduce((a,b) => a - b) are both 0. What genuinely differs is the third and fourth callback arguments — reduceRight reports the real indexes and the original array, while reversing first reports reversed indexes and the copy — plus the fact that reverse() mutates in place, so use toReversed() if you go that route.

Does reduceRight() throw on an empty array?

Yes, with the same TypeError as reduce() when no initialValue is supplied. Passing an explicit seed makes the empty case return that seed instead.

More examples

Traversal order changes the result

reduceRight prints [4, 5, 2, 3, 0, 1] and reduce prints [0, 1, 2, 3, 4, 5]. With no initialValue the right fold seeds the accumulator with the last sub-array, so each earlier chunk is appended after it.

Example · javascript
const pairs = [[0, 1], [2, 3], [4, 5]];

console.log(pairs.reduceRight((a, b) => a.concat(b)));
console.log(pairs.reduce((a, b) => a.concat(b)));

compose() versus pipe()

Prints 64. Read right to left: inc(3) is 4, dbl gives 8, sq gives 64. Swapping reduceRight for reduce would give pipe() semantics and evaluate sq first, producing a different number.

Example · javascript
const inc = (n) => n + 1;
const dbl = (n) => n * 2;
const sq = (n) => n * n;

const compose = (...fns) => (x) => fns.reduceRight((v, f) => f(v), x);

console.log(compose(sq, dbl, inc)(3));

Discussion

  • Be the first to comment on this lesson.