typedArray.reduceRight(callback, initialValue) works like reduce(), but it walks over the elements from right to left: from the last index down to index 0. The callback receives an accumulator and the current element, and returns the next accumulator. The final accumulator is the result. Without an initial value, the last element is the starting accumulator, and an empty typed array throws a TypeError.
Remember
typedArray.reduceRight(callback, initialValue) → one value
callback(accumulator, value, index, array)
with initialValue : starts at the last index
without it : acc = last element, starts at length - 2
empty, no initial : TypeError
new Uint8Array([1, 2, 3]).reduceRight((s, v) => s + v, '') → '321'
Syntax
Pass a combining function. The initialValue is optional, but it is the safe choice.
Join the elements into a string, starting from the end.
ta.reduceRight((acc, v) => acc * 2 + v, 0)
Build a number where the first element is the lowest digit.
ta.reduceRight((a, b) => a - b)
Subtract from the last element backwards.
Built into JavaScript since ES2015 on all 11 typed array types. On a BigInt array the initial value must be a BigInt, such as 0n.
Library declarations
JavaScript
TypedArray.prototype.reduceRight(callbackFn, initialValue?) // returns the final accumulator
// callbackFn(accumulator, value, index, array) -> next accumulator
// order: index length - 1 down to 0
// empty typed array and no initialValue -> throws TypeError
Parameters
Parameter
Description
callback
A function called with (accumulator, value, index, array). Return the new accumulator.
initialValue
Optional. The first accumulator. If given, the callback starts at the last index. If missing, the last element is used and the callback starts at length - 2.
Return Value
Return Value
Description
Any value
The final accumulator. It can be a number, a string, an object, or anything the callback returns. For an empty typed array with an initial value, it is that initial value.
JavaScript Examples
Example 1 — See the Order With a String
Building a string shows the order in which the elements are visited. We compare reduce() with reduceRight() on the same four numbers.
Left to right subtracts 10 and 20 from 5.reduce() computes 5 - 10 - 20 and gets -25.
Right to left subtracts 10 and 5 from 20.reduceRight() starts with 20, then takes away 10 and 5, and gets 5.
No elements means no start. Without an initial value the empty typed array throws a TypeError. With -1 it just returns -1.
Compatibility
Browser & Runtime Support
TypedArray.prototype.reduceRight() is part of ES2015 (ES6), together with the other array-like methods of typed arrays.
✓ Baseline · ES2015
TypedArray.prototype.reduceRight()
Supported in every modern browser, plus Node.js, Deno, and Bun.
98%Modern browser support
Google ChromeAll modern versions
Full support
Mozilla FirefoxAll modern versions
Full support
Apple SafariAll modern versions
Full support
Microsoft EdgeAll modern versions
Full support
Node.jsAll current releases
Supported
DenoAll releases
Supported
Internet ExplorerIE 10 and 11 have typed arrays, but not this method
Not supported
reduceRight()Excellent
Bottom line: Safe everywhere modern JavaScript runs. Internet Explorer 10 and 11 have typed arrays but not this method, so there you would loop backwards with a plain for loop.
Frequently Asked Questions
It combines all elements into one value, like reduce(), but visits them from the last element down to the first one.
When the order changes the answer: subtraction, building a string back to front, or reading digits stored lowest first.
The typed array is empty and no initial value was given. There is no last element to start with. Pass an initial value as the second argument.
No. The index is the real position from the left. Because the walk goes backwards, the first index you see is length - 1 (or length - 2 with no initial value).
No. It only reads the elements. To reverse a typed array, use reverse() or toReversed().