Array.prototype.reduceRight()
reduceRight(callbackFn, initialValue) Returns:
any · Added in vES5 · Updated March 13, 2026 · Array Methods array reduce functional
The reduceRight() method applies a callback function against an accumulator and each element in the array (from right to left) to reduce it to a single value.
Syntax
reduceRight(callbackFn)
reduceRight(callbackFn, initialValue)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
callbackFn | Function | — | Function to execute for each element. Receives accumulator, currentValue, currentIndex, and array. |
accumulator | any | — | Value accumulated across iterations. First call uses initialValue if provided, otherwise array[last]. |
currentValue | any | — | Value of current element. |
currentIndex | number | — | Index of current element. |
array | Array | — | The array being reduced. |
initialValue | any | undefined | Starting value for accumulator. |
Examples
Flattening nested arrays
const nested = [[1, 2], [3, 4], [5, 6]];
const flat = nested.reduceRight((acc, curr) => acc.concat(curr), [Global_Objects::eval]);
console.log(flat); // [5, 6, 3, 4, 1, 2]
Compare with reduce() which would produce [1, 2, 3, 4, 5, 6].
Difference between reduce and reduceRight
// reduce processes left to right
[1, 2, 3].reduce((acc, x) => [...acc, x * 2], [Global_Objects::eval]);
// Result: [2, 4, 6]
// reduceRight processes right to left
[1, 2, 3].reduceRight((acc, x) => [...acc, x * 2], [Global_Objects::eval]);
// Result: [6, 4, 2]
Common Patterns
Right-associative operations: When the operation is not commutative, like subtraction or division.
// Subtraction with reduce (left to right)
[10, 5, 2].reduce((a, b) => a - b); // (10 - 5) - 2 = 3
// Subtraction with reduceRight (right to left)
[10, 5, 2].reduceRight((a, b) => a - b); // (10 - (5 - 2)) = 7
When to Use reduceRight
Use reduceRight() when:
- The operation is not commutative (subtraction, division)
- You need to process from right to left
- You’re working with nested structures where order matters
See Also
- array::map — Transform each element
- array::filter — Select elements by condition