typedArray.forEach(callback) runs a function once for each element of a typed array, in index order. The callback receives (value, index, array). The method is used only for its side effects such as printing or adding to a counter: it always returns undefined. You cannot stop it with break or return. A typed array has no holes, so every index is visited.
Remember
typedArray.forEach(callback, thisArg) → undefined
callback(value, index, array)
visits every index, 0 to length - 1
cannot break or stop early
array = the typed array itself
new Uint8Array([1, 2]).forEach((v) => console.log(v))
Syntax
Pass a function that is called for each element. The second argument thisArg is optional.
Also get the typed array itself, for example to change an element.
Built into JavaScript since ES2015 on all 11 typed array types. Use for...of instead when you need break, continue, or await inside the loop.
Library declarations
JavaScript
TypedArray.prototype.forEach(callbackFn, thisArg?) // returns undefined
// callbackFn(value, index, array) -> return value is ignored
// every index from 0 to length - 1 is visited
Parameters
Parameter
Description
callback
A function called once per element with (value, index, array). Its return value is ignored.
thisArg
Optional. The value used as this inside the callback. Arrow functions ignore it.
Return Value
Return Value
Description
Nothing (undefined)
forEach() returns nothing. To get a result, use map(), filter(), or reduce() instead.
JavaScript Examples
Example 1 — Print Each Element and Its Number
A Uint8Array holds three scores. The callback prints each one, and we check what forEach() itself returns.
JavaScript
const scores = new Uint8Array([90, 75, 60]);
const result = scores.forEach((v, i) => {
console.log('score ' + (i + 1) + ': ' + v);
});
console.log(typeof result); // undefined
Array visits: 0
Uint8Array visits: 3
first above 10: 15
How It Works
An empty-slot array is skipped.new Array(3) has three empty slots and no real elements, so its forEach() never calls the function.
A new typed array is full of zeros.new Uint8Array(3) stores three real zeros, so all three indexes are visited and the counter reaches 3.
break lives in for...of. The loop stops at 15, the first value above 10, so 16 is never read. A forEach() callback has no way to do this.
Compatibility
Browser & Runtime Support
TypedArray.prototype.forEach() is part of ES2015 (ES6), the edition that gave typed arrays the same looping methods as regular arrays.
✓ Baseline · ES2015
TypedArray.prototype.forEach()
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
forEach()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
It always returns undefined. The method exists to run code for each element. If you want a result, use map(), filter(), or reduce().
No. A return inside the callback only ends the current call, and the next element is still visited. Use a for...of loop with break, or some() and every(), to stop early.
A typed array has no empty slots. Every index from 0 to length - 1 holds a real number, so the callback runs for every one of them.
Changing elements through the array argument is safe, because the length can never change. The new value is converted to the element type, so a Uint8Array wraps it.
Both are fine for small typed arrays. A classic for loop or for...of is more flexible, since it supports break, continue, and await inside async functions.