The forEach()
method executes a provided function once per array element. This method has the same algorithm as Array.prototype.forEach()
. TypedArray is one of the typed array types here.
The forEach()
method executes a provided function once per array element. This method has the same algorithm as Array.prototype.forEach()
. TypedArray is one of the typed array types here.
// Arrow function forEach((element) => { /* ... */ } ) forEach((element, index) => { /* ... */ } ) forEach((element, index, array) => { /* ... */ } ) // Callback function forEach(callbackFn) forEach(callbackFn, thisArg) // Inline callback function forEach(function(element) { /* ... */ }) forEach(function(element, index) { /* ... */ }) forEach(function(element, index, array){ /* ... */ }) forEach(function(element, index, array) { /* ... */ }, thisArg)
callbackFn
Function that produces an element of the new typed array.
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 array.
array
The array forEach()
was called upon.
thisArg
Optional
Value to use as this
when executing callbackFn
.
The forEach()
method executes the provided callbackFn
once for each element present in the typed array in ascending order. It is not invoked for indexes that have been deleted or elided. However, it is executed for elements that are present and have the value undefined
.
callbackFn
is invoked with three arguments:
If a thisArg
parameter is provided to forEach()
, it will be passed to callbackFn
when invoked, for use as its this
value. Otherwise, the value undefined
will be passed for use as its this
value. The this
value ultimately observable by callbackFn
is determined according to the usual rules for determining the this
seen by a function.
The range of elements processed by forEach()
is set before the first invocation of callbackFn
. Elements that are appended to the typed array after the call to forEach()
begins will not be visited by callbackFn
. If the values of existing elements of the typed array are changed, the value passed to callbackFn
will be the value at the time forEach()
visits them; elements that are deleted before being visited are not visited.
forEach()
executes the callbackFn
function once for each typed array element; unlike every()
and some()
it, always returns the value undefined
.
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
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
forEach |
45 |
12 |
38 |
No |
32 |
10 |
45 |
45 |
38 |
32 |
10 |
5.0 |
1.0 |
4.0.0 |
© 2005–2022 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