Array.prototype.forEach()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/forEach
  • Revision title: Array forEach method
  • Revision id: 1536
  • Created:
  • Creator: Anonymous
  • Is current revision? No
  • Comment /* Description */

Revision Content

Summary

Executes a provided function once per array element.

Method of Array
Implemented in: JavaScript 1.5 (Gecko 1.8b2 and later)
ECMA Version: none

Syntax

forEach(callback{{mediawiki.external(', <i>thisObject</i>')}})

Parameters

callback 
Function to execute for each element.
thisObject 
Object to use as this when executing callback.

Description

forEach executes the provided function (callback) once for each element present in the array. Arrays are treated as "dense", meaning that callback will be invoked for each index less than the array's length, even if a given index has never been explicitly assigned.

callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.

If a thisObject parameter is provided to forEach, it will be used as the this for each invocation of the callback. If it is not provided, or is null, the global object associated with callback is used instead.

forEach does not mutate the array on which it is called.

The set of elements processed by forEach is set before the first invocation of callback. Elements which are appended to the array after the call to forEach begins will not be visited by callback. If existing elements of the array are changed, or deleted, their value as passed to callback will be the value at the time forEach visits them; elements that are deleted will have the value undefined.

Examples

Example: Printing the contents of an array

The following code prints a line for each element in an array (and presumes the presence of a print function to call!):

var array = [2, 5, 9];
function printElt(element, index) {
    print("[" + index + "] is " + element);
}
array.forEach(printElt);
// Prints:
// [0] is 2
// [1] is 5
// [2] is 9

Example: Printing the contents of an array with an object method

TODO

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Executes a provided function once per array element.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Objects/Array">Array</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.5 (Gecko 1.8b2 and later)</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>none</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
forEach(<i>callback</i>{{mediawiki.external(', &lt;i&gt;thisObject&lt;/i&gt;')}})
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>callback</code> </dt><dd> Function to execute for each element.
</dd><dt> <code>thisObject</code> </dt><dd> Object to use as <code>this</code> when executing <code>callback</code>.
</dd></dl>
<h3 name="Description"> Description </h3>
<p><code>forEach</code> executes the provided function (<code>callback</code>) once for each element present in the array.  Arrays are treated as "dense", meaning that <code>callback</code> will be invoked for each index less than the array's <code>length</code>, even if a given index has never been explicitly assigned.
</p><p><code>callback</code> is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.
</p><p>If a <code>thisObject</code> parameter is provided to <code>forEach</code>, it will be used as the <code>this</code> for each invocation of the <code>callback</code>.  If it is not provided, or is <code>null</code>, the global object associated with <code>callback</code> is used instead.
</p><p><code>forEach</code> does not mutate the array on which it is called.
</p><p>The set of elements processed by <code>forEach</code> is set before the first invocation of <code>callback</code>.  Elements which are appended to the array after the call to <code>forEach</code> begins will not be visited by <code>callback</code>.  If existing elements of the array are changed, or deleted, their value as passed to <code>callback</code> will be the value at the time <code>forEach</code> visits them; elements that are deleted will have the value <code>undefined</code>.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Printing_the_contents_of_an_array"> Example: Printing the contents of an array </h4>
<p>The following code prints a line for each element in an array (and presumes the presence of a <code>print</code> function to call!):
</p>
<pre>var array = [2, 5, 9];
function printElt(element, index) {
    print("[" + index + "] is " + element);
}
array.forEach(printElt);
// Prints:
// [0] is 2
// [1] is 5
// [2] is 9
</pre>
<h4 name="Example:_Printing_the_contents_of_an_array_with_an_object_method"> Example: Printing the contents of an array with an object method </h4>
<p><i>TODO</i>
</p>
Revert to this revision