TypedArray.prototype.forEach()
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.
Syntax
<var>typedarray</var>.forEach(<var>callback</var>[, <var>thisArg</var>])
Parameters
callback
- Function that produces an element of the new typed array, taking three arguments:
currentValue
- 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 executingcallback
.
Return value
Description
The forEach()
method executes the provided callback
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
.
callback
is invoked with three arguments:
- the element value
- the element index
- the typed array being traversed
If a thisArg
parameter is provided to forEach()
, it will be passed to callback
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 callback
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 callback
. Elements that are appended to the typed array after the call to forEach()
begins will not be visited by callback
. If the values of existing elements of the typed array are changed, the value passed to callback
will be the value at the time forEach()
visits them; elements that are deleted before being visited are not visited.
forEach()
executes the callback
function once for each typed array element; unlike every()
and some()
it, always returns the value undefined
.
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 | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of '%TypedArray%.prototype.forEach' in that specification. |
Standard | Initial definition. |
ECMAScript 2017 Draft (ECMA-262) The definition of '%TypedArray%.prototype.forEach' in that specification. |
Draft |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | ? | 38 (38) | ? | ? | ? |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | ? | 38.0 (38) | ? | ? | ? |
See also
TypedArray.prototype.map()
TypedArray.prototype.every()
TypedArray.prototype.some()
Array.prototype.forEach()
License
© 2016 Mozilla 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