JavaScript TypedArray forEach() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2015

Definition

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.

JavaScript
typedArray.forEach(callback)
typedArray.forEach(callback, thisArg)
CallWhat it does
ta.forEach((v) => ...)Run code for each value.
ta.forEach((v, i) => ...)Also get the index of the element.
ta.forEach((v, i, arr) => ...)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

ParameterDescription
callbackA function called once per element with (value, index, array). Its return value is ignored.
thisArgOptional. The value used as this inside the callback. Arrow functions ignore it.

Return Value

Return ValueDescription
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
Try It Yourself

How It Works

  1. Value and index arrive together. The callback gets the element as v and its position as i, so the message can number the scores from 1.
  2. One call per element. Three elements mean three calls, in the order 90, 75, 60. Nothing is skipped.
  3. The result is nothing. The returned value is stored in result, and typeof result prints undefined, because the method hands back no data.

Example 2 — Change Elements Through the array Argument

The third callback argument is the typed array itself. Writing to it changes the data in place, and the type still applies its wrapping.

JavaScript
const pixels = new Uint8Array([10, 100, 130]);

pixels.forEach((v, i, arr) => {
  arr[i] = v * 2;
});

console.log(pixels.join(' '));  // 20 200 4
Try It Yourself

How It Works

  1. arr is the same typed array. The third argument points at pixels, so arr[i] = ... writes into the original bytes. No copy is made.
  2. Double each byte. The value 10 becomes 20 and the value 100 becomes 200, both of which fit in one byte.
  3. A Uint8Array wraps. The value 130 doubled is 260, which does not fit in a byte, so the typed array stores 260 - 256 = 4.

Example 3 — No Holes, and Stopping Early With for...of

A regular array can have empty slots that forEach() skips. A typed array cannot. For an early stop, a for...of loop is the right tool.

JavaScript
let arrayVisits = 0;
new Array(3).forEach(() => { arrayVisits++; });

let typedVisits = 0;
new Uint8Array(3).forEach(() => { typedVisits++; });

console.log('Array visits: ' + arrayVisits);       // Array visits: 0
console.log('Uint8Array visits: ' + typedVisits);  // Uint8Array visits: 3

const data = new Uint8Array([4, 8, 15, 16]);
let firstBig = -1;
for (const v of data) {
  if (v > 10) {
    firstBig = v;
    break;
  }
}
console.log('first above 10: ' + firstBig);  // first above 10: 15
Try It Yourself

How It Works

  1. An empty-slot array is skipped. new Array(3) has three empty slots and no real elements, so its forEach() never calls the function.
  2. 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.
  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.

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 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
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.

Next: includes()

Check whether a typed array contains a value, even NaN on float arrays.

includes() 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