UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ TypedArray/ TypedArray.prototype.forEach()

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.

Syntax

forEach(callbackFn)
forEach(callbackFn, thisArg)

Parameters

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

Browser compatibility

See also