Array.prototype.reduceRight
Applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value
Syntax
array.reduceRight(callbackFn, initialValue?)Parameters
| Parameter | Type | Description |
|---|---|---|
| callbackFn | (accumulator, currentValue, index, array) => T | Function to execute on each element |
| initialValue | T | Initial value for the accumulator |
Return Value
The value resulting from the reduction
Examples
const arr = [[1, 2], [3, 4], [5, 6]];
const flat = arr.reduceRight((acc, cur) => acc.concat(cur), [] as number[]);
console.log(flat); // [5, 6, 3, 4, 1, 2]const pipe = [
(x: number) => x + 1,
(x: number) => x * 2,
(x: number) => x - 3,
];
const result = pipe.reduceRight((val, fn) => fn(val), 5);
console.log(result); // ((5 - 3) * 2) + 1 = 5const words = ['world', ' ', 'hello'];
const sentence = words.reduceRight((acc, w) => acc + w, '');
console.log(sentence); // 'hello world'Understanding Array.prototype.reduceRight
The Array.prototype.reduceRight method in JavaScript applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value. It belongs to the Array object and is one of the most widely used methods for working with array values in modern JavaScript and TypeScript applications.
The method signature is array.reduceRight(callbackFn, initialValue?). It accepts 2 parameters: callbackFn, initialValue. When called, it returns the value resulting from the reduction. Understanding when and how to use reduceRight() helps you write more expressive, readable code.
Common use cases for Array.prototype.reduceRight include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like array-reduce, array-map, array-filter, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Array.prototype.reduceRight is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Array.prototype.reduceExecutes a user-supplied reducer callback function on each element of the array, passing in the return value from the calculation on the preceding element
Array.prototype.mapCreates a new array populated with the results of calling a provided function on every element in the calling array
Array.prototype.filterCreates a shallow copy of a portion of a given array, filtered down to just the elements that pass the test implemented by the provided function
More Array Methods
Other methods in the Array object
Related Tools
More Array Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.