Array.prototype.reduceRight
Reduces an array to one value, going from right to left.
Available since: ES5
Syntax
array.reduceRight(callbackFn, initialValue)Parameters
| Parameter | Type | Description |
|---|---|---|
| callbackFn | Function | A function that receives the accumulator and the current element and returns the new accumulator. |
| initialValue | any | Optional. The starting value of the accumulator. |
Return value
anyThe final accumulated value.
Examples
Joining in reverse order:
console.log(['a', 'b', 'c'].reduceRight((acc, letter) => acc + letter, ''));Output:
cbaNotes
Works like reduce() but from the end of the array. Rarely needed — usually to compose functions right to left.
See also
- Array.prototype.reduce — Reduces an array to a single value.
- Array.prototype.reverse — Reverses an array in place.
In the tutorial
- map, filter and reduce — The three main array processing methods in JavaScript: transforming, selecting and reducing to a single value.