Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 784277 of for each...in

  • Revision slug: Web/JavaScript/Reference/Statements/for_each...in
  • Revision title: for each...in
  • Revision id: 784277
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment lint

Revision Content

{{jsSidebar("Statements")}}

The for each...in statement is deprecated as the part of ECMA-357 (E4X) standard. E4X support has been removed, but for each...in will not be disabled and removed because of backward compatibility considerations. Consider using for...of instead. (Please refer to {{ bug("791343")}}.)

The for each...in statement iterates a specified variable over all values of object's properties. For each distinct property, a specified statement is executed.

Syntax

for each (variable in object) {
  statement
}
variable
Variable to iterate over property values, optionally declared with the var keyword. This variable is local to the function, not to the loop.
object
Object for which the properties are iterated.
statement
A statement to execute for each property. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements.

Description

Some built-in properties are not iterated over. These include all built-in methods of objects, e.g. String's indexOf method. However, all user-defined properties are iterated over.

Examples

Using for each...in

Warning: Never use a loop like this on arrays. Only use it on objects. See for...in for more details.

The following snippet iterates over an object's properties, calculating their sum:

var sum = 0;
var obj = {prop1: 5, prop2: 13, prop3: 8};

for each (var item in obj) {
  sum += item;
}

console.log(sum); // logs "26", which is 5+13+8

Specifications

Not part of a current ECMA-262 specification. Implemented in JavaScript 1.6 and deprecated.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatNo}} {{CompatGeckoDesktop("1.8")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatNo}} {{CompatGeckoMobile("1.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

See also

  • for...in - a similar statement that iterates over the property names.
  • for...of - a similar statement that iterates over the property values but there is some behavior that is different from for each...in statements.
  • for

Revision Source

<div>{{jsSidebar("Statements")}}</div>

<div class="warning">
<p>The <code>for each...in</code> statement is deprecated as the part of ECMA-357 (<a href="/en-US/docs/Archive/Web/E4X" title="/en-US/docs/E4X">E4X</a>) standard. E4X support has been removed, but for each...in will not be disabled and removed because of backward compatibility considerations. Consider using <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of" title="/en-US/docs/JavaScript/Reference/Statements/for...of">for...of</a> instead. (Please refer to {{ bug("791343")}}.)</p>
</div>

<p>The<code> <strong>for each...in</strong></code><strong> statement</strong> iterates a specified variable over all values of object's properties. For each distinct property, a specified statement is executed.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
<code>for each (<em>variable</em> in <em>object</em>) {
  <em>statement</em>
}</code></pre>

<dl>
 <dt><code>variable</code></dt>
 <dd>Variable to iterate over property values, optionally declared with the <code>var</code> keyword. This variable is local to the function, not to the loop.</dd>
</dl>

<dl>
 <dt><code>object</code></dt>
 <dd>Object for which the properties are iterated.</dd>
</dl>

<dl>
 <dt><code>statement</code></dt>
 <dd>A statement to execute for each property. To execute multiple statements within the loop, use a <a href="/en-US/docs/Web/JavaScript/Reference/Statements/block" title="JavaScript/Reference/Statements/block">block</a> statement (<code>{ ... }</code>) to group those statements.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>Some built-in properties are not iterated over. These include all built-in methods of objects, e.g. <code>String</code>'s <code>indexOf</code> method. However, all user-defined properties are iterated over.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Using_for_each...in">Using <code>for each...in</code></h3>

<p><strong>Warning:</strong> Never use a loop like this on arrays. Only use it on objects. See <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in"><code>for...in</code></a> for more details.</p>

<p>The following snippet iterates over an object's properties, calculating their sum:</p>

<pre class="brush:js">
var sum = 0;
var obj = {prop1: 5, prop2: 13, prop3: 8};

for each (var item in obj) {
  sum += item;
}

console.log(sum); // logs "26", which is 5+13+8</pre>

<h2>Specifications</h2>

<p>Not part of a current ECMA-262 specification. Implemented in JavaScript 1.6 and deprecated.</p>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop("1.8")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile("1.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in" title="JavaScript/Reference/Statements/for...in">for...in</a></code> - a similar statement that iterates over the property <em>names</em>.</li>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of" title="/en-US/docs/JavaScript/Reference/Statements/for...of">for...of</a></code> - a similar statement that iterates over the property <em>values</em> but there is some behavior that is different from <code>for each...in</code> statements.</li>
 <li><code><a href="/en-US/docs/JavaScript/Reference/Statements/for" title="JavaScript/Reference/Statements/for">for</a></code></li>
</ul>
Revert to this revision