JavaScript TypedArray reduceRight() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2015

Definition

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.

JavaScript
typedArray.reduceRight(callback)
typedArray.reduceRight(callback, initialValue)
CallWhat it does
ta.reduceRight((s, v) => s + v, '')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

ParameterDescription
callbackA function called with (accumulator, value, index, array). Return the new accumulator.
initialValueOptional. 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 ValueDescription
Any valueThe 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.

JavaScript
const digits = new Uint8Array([1, 2, 3, 4]);

const fromLeft = digits.reduce((s, v) => s + v, '');
const fromRight = digits.reduceRight((s, v) => s + v, '');

console.log('reduce:      ' + fromLeft);   // reduce:      1234
console.log('reduceRight: ' + fromRight);  // reduceRight: 4321
Try It Yourself

How It Works

  1. The empty string is the start. Both calls begin with '' and add one number after another to its end.
  2. Left to right gives 1234. reduce() adds 1, 2, 3, and 4 in that order, so the string grows in index order.
  3. Right to left gives 4321. reduceRight() starts with the last element 4, so the same numbers appear in reverse.

Example 2 — Build a Number and Trace the Indexes

Four bits are stored with the lowest bit first. Reading from the right builds the number. A second call records which indexes the callback sees.

JavaScript
const bits = new Uint8Array([1, 1, 0, 1]);

const value = bits.reduceRight((acc, bit) => acc * 2 + bit, 0);
console.log('1011 in binary is ' + value);  // 1011 in binary is 11

const seen = [];
bits.reduceRight((acc, v, i) => {
  seen.push(i);
  return acc + v;
});
console.log('indexes: ' + seen.join(' '));  // indexes: 2 1 0
Try It Yourself

How It Works

  1. The highest bit goes in first. Starting at 0, the steps are 0 * 2 + 1, then 1 * 2 + 0, then 2 * 2 + 1, and finally 5 * 2 + 1, which is 11.
  2. No initial value skips one index. In the second call the last element 1 is already the accumulator, so the callback is not run for index 3.
  3. Indexes still count from the left. The callback sees index 2, then 1, then 0: the visiting goes down, but each index is the real position.

Example 3 — Direction Matters, and Empty Arrays Throw

Subtraction gives different answers in each direction. An empty typed array needs an initial value.

JavaScript
const queue = new Int16Array([5, 10, 20]);

console.log(queue.reduce((a, b) => a - b));       // -25
console.log(queue.reduceRight((a, b) => a - b));  // 5

const empty = new Int16Array(0);
try {
  empty.reduceRight((a, b) => a + b);
} catch (e) {
  console.log(e.name);  // TypeError
}
console.log(empty.reduceRight((a, b) => a + b, -1));  // -1
Try It Yourself

How It Works

  1. Left to right subtracts 10 and 20 from 5. reduce() computes 5 - 10 - 20 and gets -25.
  2. Right to left subtracts 10 and 5 from 20. reduceRight() starts with 20, then takes away 10 and 5, and gets 5.
  3. No elements means no start. Without an initial value the empty typed array throws a TypeError. With -1 it just returns -1.

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 Chrome All modern versions
Full support
Mozilla Firefox All modern versions
Full support
Apple Safari All modern versions
Full support
Microsoft Edge All modern versions
Full support
Node.js All current releases
Supported
Deno All releases
Supported
Internet Explorer IE 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().

Next: reverse()

Reverse the order of the elements of a typed array in place.

reverse() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful