Spec-Zone.ru › JavaScript

TypedArray.prototype.forEach()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It's been available across browsers since September 2016.

The forEach() method of TypedArray instances executes a provided function once for each typed array element. This method has the same algorithm as Array.prototype.forEach().

Try it

const uint8 = new Uint8Array([10, 20, 30]);

uint8.forEach((element) => console.log(element));

// Expected output: 10
// Expected output: 20
// Expected output: 30

Syntax

forEach(callbackFn)
forEach(callbackFn, thisArg)

Parameters

callbackFn
A function to execute for each element in the typed array. Its return value is discarded. The function is called with the following arguments:
element
The current element being processed in the typed array.
index
The index of the current element being processed in the typed array.
array
The typed array forEach() was called upon.
thisArg Optional
A value to use as this when executing callbackFn. See iterative methods.

Return value

None (undefined).

Description

See Array.prototype.forEach() for more details. This method is not generic and can only be called on typed array instances.

Examples

Logging the contents of a typed array

The following code logs a line for each element in a typed array:

function logArrayElements(element, index, array) {
  console.log(`a[${index}] = ${element}`);
}

new Uint8Array([0, 1, 2, 3]).forEach(logArrayElements);
// Logs:
// a[0] = 0
// a[1] = 1
// a[2] = 2
// a[3] = 3

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-%typedarray%.prototype.foreach

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on iOS Samsung Internet WebView Android WebView on iOS Bun Deno Node.js
forEach
45
12
38
32
10
45
38
32
10
5.0
45
10
1.0.0
1.0
4.0.0

See also

  • Polyfill of TypedArray.prototype.forEach in core-js
  • JavaScript typed arrays guide
  • TypedArray
  • TypedArray.prototype.find()
  • TypedArray.prototype.map()
  • TypedArray.prototype.filter()
  • TypedArray.prototype.every()
  • TypedArray.prototype.some()
  • Array.prototype.forEach()
  • Map.prototype.forEach()
  • Set.prototype.forEach()

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/forEach

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API