typedArray.reduce(callback, initialValue) walks over the elements from left to right and combines them into one value. The callback receives an accumulator (the result so far) and the current element, and its return value becomes the next accumulator. The accumulator is a normal JavaScript value, so it does not wrap like the elements do. Without an initial value, the first element is the starting accumulator, and an empty typed array throws a TypeError.
Remember
typedArray.reduce(callback, initialValue) → one value
callback(accumulator, value, index, array)
with initialValue : starts at index 0
without it : starts at index 1, acc = element 0
empty, no initial : TypeError
new Uint8Array([200, 150]).reduce((s, v) => s + v) → 350
Syntax
Pass a combining function. The initialValue is optional, but it is the safe choice.
Find the largest element without an initial value.
ta.reduce((best, v, i, arr) => ..., 0)
Use the index and the typed array too.
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.reduce(callbackFn, initialValue?) // returns the final accumulator
// callbackFn(accumulator, value, index, array) -> next accumulator
// 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 index 0. If missing, the first element is used and the callback starts at index 1.
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 — Add Up the Elements
Three bytes are added with and without an initial value. The total is a normal number, so it is allowed to be bigger than 255.
JavaScript
const bytes = new Uint8Array([200, 150, 100]);
const total = bytes.reduce((sum, v) => sum + v);
console.log('total: ' + total); // total: 450
const withStart = bytes.reduce((sum, v) => sum + v, 1000);
console.log('with start: ' + withStart); // with start: 1450
The first element starts the sum. With no initial value, sum begins as 200 and the callback adds 150 and then 100.
No wrapping in the accumulator. The result 450 is above 255, but it lives in a plain JavaScript number and not in a Uint8Array, so it is not cut off.
An initial value goes first. With 1000 as the second argument, the callback runs for all three bytes, and the total is 1450.
Example 2 — Find the Maximum, Minimum, and Position
An Int16Array holds temperatures. reduce() can keep a record of the best element so far, or of its index.
JavaScript
const temps = new Int16Array([12, -7, 31, 4, -15]);
const hi = temps.reduce((a, b) => (a > b ? a : b));
console.log('max: ' + hi); // max: 31
const lo = temps.reduce((a, b) => Math.min(a, b), Infinity);
console.log('min: ' + lo); // min: -15
const hiIndex = temps.reduce((best, v, i, arr) => (v > arr[best] ? i : best), 0);
console.log('max at index ' + hiIndex); // max at index 2
const average = temps.reduce((s, v) => s + v, 0) / temps.length;
console.log('average: ' + average); // average: 5
Keep the bigger of two. The callback returns the larger of the accumulator and the current element, so the accumulator ends as 31.
Infinity makes a safe start. The initial value Infinity is above every number, so Math.min always lets the first real element win.
Track an index and an average. The accumulator can hold the best index instead of a value, using arr[best]. Dividing the sum 25 by the length 5 gives 5.
Example 3 — Empty Typed Arrays and One Element
Two edge cases decide whether your code is safe: no elements, and exactly one element.
JavaScript
const empty = new Float32Array(0);
try {
empty.reduce((a, b) => a + b);
} catch (e) {
console.log(e.name); // TypeError
}
console.log(empty.reduce((a, b) => a + b, 0)); // 0
const one = new Uint8Array([42]);
let calls = 0;
const only = one.reduce((a, b) => {
calls++;
return a + b;
});
console.log(only + ' after ' + calls + ' calls'); // 42 after 0 calls
No start value, no element. There is nothing to use as the first accumulator, so the call throws a TypeError, and e.name prints that word.
An initial value saves the day. With 0 as the second argument, the empty typed array simply returns 0 and the callback is never called.
One element needs no combining. The single element 42 becomes the result right away, and the counter proves the callback ran zero times.
Compatibility
Browser & Runtime Support
TypedArray.prototype.reduce() is part of ES2015 (ES6), together with the other array-like methods of typed arrays.
✓ Baseline · ES2015
TypedArray.prototype.reduce()
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
reduce()Excellent
Bottom line: Safe everywhere modern JavaScript runs. Internet Explorer 10 and 11 have typed arrays but not this method, so there you would use a plain for loop.
Frequently Asked Questions
The final accumulator: one value built from all elements, such as a number, a string, or an object. It is not a typed array.
The typed array is empty and you gave no initial value. Pass one, such as 0, as the second argument.
Yes. The accumulator is a normal JavaScript number, not a byte. Only values written back into a typed array are wrapped to the element type.
Yes, it is safer. It protects against empty typed arrays and makes the callback run for every element.
reduce() goes from the first element to the last, and reduceRight() goes from the last to the first. The order matters for results like subtraction or building a string.