Array.prototype.reduceRight()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The reduceRight() method of Array instances applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.
See also Array.prototype.reduce() for left-to-right.
Try it
const array = [
[0, 1],
[2, 3],
[4, 5],
];
const result = array.reduceRight((accumulator, currentValue) =>
accumulator.concat(currentValue),
);
console.log(result);
// Expected output: Array [4, 5, 2, 3, 0, 1]
Syntax
reduceRight(callbackFn)
reduceRight(callbackFn, initialValue)
Parameters
callbackFn-
A function to execute for each element in the array. Its return value becomes the value of the
accumulatorparameter on the next invocation ofcallbackFn. For the last invocation, the return value becomes the return value ofreduceRight(). The function is called with the following arguments:accumulator-
The value resulting from the previous call to
callbackFn. On the first call, its value isinitialValueif the latter is specified; otherwise its value is the last element of the array. currentValue-
The value of the current element. On the first call, its value is the last element if
initialValueis specified; otherwise its value is the second-to-last element. currentIndex-
The index position of
currentValuein the array. On the first call, its value isarray.length - 1ifinitialValueis specified, otherwisearray.length - 2. array-
The array
reduceRight()was called upon.
initialValueOptional-
Value to use as accumulator to the first call of the
callbackFn. If no initial value is supplied, the last element in the array will be used and skipped. CallingreduceRight()on an empty array without an initial value creates aTypeError.
Return value
The value that results from the reduction.
Description
The reduceRight() method is an iterative method. It runs a "reducer" callback function over all elements in the array, in descending-index order, and accumulates them into a single value. Read the iterative methods section for more information about how these methods work in general.
callbackFn is invoked only for array indexes which have assigned values. It is not invoked for empty slots in sparse arrays.
Unlike other iterative methods, reduceRight() does not accept a thisArg argument. callbackFn is always called with undefined as this, which gets substituted with globalThis if callbackFn is non-strict.
The reduceRight() method is generic. It only expects the this value to have a length property and integer-keyed properties.
All caveats about reduce discussed in when to not use reduce() apply to reduceRight as well. Because JavaScript has no lazy evaluation semantics, there is no performance difference between reduce and reduceRight.
Examples
>How reduceRight() works without an initial value
The call to the reduceRight