TypedArray.prototype.forEach()
En esta página
The forEach() method of {{jsxref("TypedArray")}} instances executes a provided function once for each typed array element. This method has the same algorithm as {{jsxref("Array.prototype.forEach()")}}.
{{InteractiveExample("JavaScript Demo: TypedArray.prototype.forEach()")}}
```js interactive-example const uint8 = new Uint8Array([10, 20, 30]);
uint8.forEach((element) => console.log(element));
// Expected output: 10 // Expected output: 20 // Expected output: 30
## Syntax
```js-nolint
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_inline}}- : A value to use as
thiswhen executingcallbackFn. See iterative methods.
Return value#
None ({{jsxref("undefined")}}).
Description#
See {{jsxref("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#
{{Specifications}}
Browser compatibility#
{{Compat}}
See also#
- Polyfill of
TypedArray.prototype.forEachincore-js - JavaScript typed arrays guide
- {{jsxref("TypedArray")}}
- {{jsxref("TypedArray.prototype.find()")}}
- {{jsxref("TypedArray.prototype.map()")}}
- {{jsxref("TypedArray.prototype.filter()")}}
- {{jsxref("TypedArray.prototype.every()")}}
- {{jsxref("TypedArray.prototype.some()")}}
- {{jsxref("Array.prototype.forEach()")}}
- {{jsxref("Map.prototype.forEach()")}}
- {{jsxref("Set.prototype.forEach()")}}