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

ParameterTypeDefaultDescription
callbackFnFunctionFunction to execute for each element. Receives accumulator, currentValue, currentIndex, and array.
accumulatoranyValue accumulated across iterations. First call uses initialValue if provided, otherwise array[last].
currentValueanyValue of current element.
currentIndexnumberIndex of current element.
arrayArrayThe array being reduced.
initialValueanyundefinedStarting 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