Array.prototype.forEach()

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

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 two arguments: the value of the element, and the index of the element. (callback need not declare both, or indeed either, of these arguments if they are not used.)

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; if the array is mutated while forEach is executing, including during callback invocation, the results are undefined (especially how many times the callback will be invoked, and for what index/element pairs).

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 two arguments: the value of the element, and the index of the element.  (<code>callback</code> need not declare both, or indeed either, of these arguments if they are not used.)
</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; if the array is mutated while forEach is executing, including during <code>callback</code> invocation, the results are undefined (especially how many times the <code>callback</code> will be invoked, and for what index/element pairs).
</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