for...in

  • Revision slug: JavaScript/Reference/Statements/for...in
  • Revision title: for...in
  • Revision id: 7884
  • Created:
  • Creator: niels_bom
  • Is current revision? No
  • Comment fixed typo; 1 words added, 1 words removed

Revision Content

Summary

Iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.

Statement
Implemented in: JavaScript 1.0, NES 2.0
ECMA Version: ECMA-262

Syntax

for (variable in object) {
  ...
} 

Parameters

variable
A different property name is assigned to variable on each iteratation.
object
Object whose enumerable properties are iterated.

Description

A for...in loop does not iterate over non–enumerable properties. Objects created from built–in constructors like Array and Object have inherited non–enumerable properties from Object.prototype and String.protottype that are not enumerable, such as String's indexOf method or Object's toString method. The loop will iterate over all enumerable properties of the object or that it inherits from its constructor's proptotype (including any which overwrite built-in properties).

A for...in loop iterates over the properties of an object in an arbitrary order (see the delete operator for more on why one cannot depend on the seeming orderliness of iteration, at least in a cross-browser setting). If a property is modified in one iteration and then visited at a later time, its value in the loop is its value at that later time. A property that is deleted before it has been visited will not be visited later. Properties added to the object over which iteration is occurring may either be visited or omitted from iteration. In general it is best not to add, modify or remove properties from the object during iteration, other than the property currently being visited. There is no guarantee whether or not an added property will be visited, whether a modified property (other than the current one) will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.

for..in should not be used to iterate over an Array where index order is important. Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties.  There is no guarantee that for...in will return the indexes in any particular order and it will return all enumerable properties, including those with non–integer names and those that are inherited.

Because the order of iteration is implementation dependent, iterating over an array may not visit elements in a consistent order. Therefore it is better to use a for loop with a numeric index when iterating over arrays where the order of access is important.

Where only the properties of the object should be considered, a hasOwnProperty check should be performed to ensure that only properties of the object and not inherited properties are used (propertyIsEnumerable can also be used but is not intuitive).

In some cases, built–in prototypes be usefully extended by the user where there are no concerns caused by inclusion of other code that may use for..in  and not perform the above checks..

Example

The following function takes as its arguments an object and the object's name. It then iterates over all the object's enumerable properties and returns a string of the property names and their values.

var o = {a:1, b:2, c:3};

function show_props(obj, objName) {  
   var result = "";  
   for (var prop in obj) {  
      result += objName + "." + prop + " = " + obj[prop] + "\n";  
   }  
   return result;  
}  

alert(show_props(o, "o"));
/* alerts :
o.a = 1
o.b = 2
o.c = 3
*/

See also

{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Sentencias/for...in", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/for...in", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/for...in", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/for...in" } ) }}

Revision Source

<h2 name="Summary">Summary</h2>
<p>Iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.</p>
<table class="fullwidth-table"> <tbody> <tr> <td class="header" colspan="2">Statement</td> </tr> <tr> <td>Implemented in:</td> <td>JavaScript 1.0, NES 2.0</td> </tr> <tr> <td>ECMA Version:</td> <td>ECMA-262</td> </tr> </tbody>
</table>
<h2 name="Syntax">Syntax</h2>
<pre class="eval">for (<em>variable</em> in <em>object</em>) {
  <span style="font-style: italic;">...</span>
} 
</pre>
<h2 name="Parameters">Parameters</h2>
<dl> <dt><code>variable</code></dt> <dd>A different property name is assigned to <em>variable</em> on each iteratation.</dd>
</dl>
<dl> <dt><code>object</code></dt> <dd>Object whose enumerable properties are iterated.</dd>
</dl>
<p>Description</p>
<p>A <code>for...in</code> loop does not iterate over non–enumerable properties. Objects created from built–in constructors like Array and Object have inherited non–enumerable properties from <code>Object.prototype</code> and <code>String.protottype</code> that are not enumerable, such as <code><a href="/En/Core_JavaScript_1.5_Reference/Global_Objects/String" title="en/Core_JavaScript_1.5_Reference/Global_Objects/String">String</a></code>'s <code><a href="/en/JavaScript/Reference/Global_Objects/String/indexOf" title="en/Core_JavaScript_1.5_Reference/Global_Objects/String/indexOf">indexOf</a></code> method or <code><a href="/en/JavaScript/Reference/Global_Objects/Object" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Object">Object</a></code>'s <code><a href="/en/JavaScript/Reference/Global_Objects/Object/toString" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString">toString</a></code> method. The loop will iterate over all enumerable properties of the object or that it inherits from its constructor's proptotype (including any which overwrite built-in properties).</p>
<p>A <code>for...in</code> loop iterates over the properties of an object in an arbitrary order (see the <a href="/en/JavaScript/Reference/Operators/delete#Cross-browser_issues" title="en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/delete_Operator#Cross-browser_issues">delete operator</a> for more on why one cannot depend on the seeming orderliness of iteration, at least in a cross-browser setting). If a property is modified in one iteration and then visited at a later time, its value in the loop is its value at that later time. A property that is deleted before it has been visited will not be visited later. Properties added to the object over which iteration is occurring may either be visited or omitted from iteration. In general it is best not to add, modify or remove properties from the object during iteration, other than the property currently being visited. There is no guarantee whether or not an added property will be visited, whether a modified property (other than the current one) will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.</p>
<div class="note"> <p><code>for..in</code> should not be used to iterate over an <a href="/en/JavaScript/Reference/Global_Objects/Array" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Array">Array</a> where index order is important. Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties.  There is no guarantee that <code>for...in</code> will return the indexes in any particular order and it will return all enumerable properties, including those with non–integer names and those that are inherited.</p> <p>Because the order of iteration is implementation dependent, iterating over an array may not visit elements in a consistent order. Therefore it is better to use a <a href="/en/JavaScript/Reference/Statements/for" title="en/Core_JavaScript_1.5_Reference/Statements/for">for</a> loop with a numeric index when iterating over arrays where the order of access is important.</p> <p>Where only the properties of the object should be considered, a <a class="internal" href="/en/JavaScript/Reference/Global_Objects/Object/hasOwnProperty" title="en/Core JavaScript 1.5 Reference/Global Objects/Object/hasOwnProperty">hasOwnProperty</a> check should be performed to ensure that only properties of the object and not inherited properties are used (<a class="internal" href="/en/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable" title="en/Core JavaScript 1.5 Reference/Global Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a> can also be used but is not intuitive).</p> <p>In some cases, built–in prototypes be usefully extended by the user where there are no concerns caused by inclusion of other code that may use <code>for..in</code>  and not perform the above checks..</p>
</div>
<h2 name="Example">Example</h2>
<p>The following function takes as its arguments an object and the object's name. It then iterates over all the object's enumerable properties and returns a string of the property names and their values.</p>
<pre class="brush: js">var o = {a:1, b:2, c:3};

function show_props(obj, objName) {  
   var result = "";  
   for (var prop in obj) {  
      result += objName + "." + prop + " = " + obj[prop] + "\n";  
   }  
   return result;  
}  

alert(show_props(o, "o"));
/* alerts :
o.a = 1
o.b = 2
o.c = 3
*/
</pre>
<h2 name="See_also">See also</h2>
<ul> <li><a href="/en/JavaScript/Reference/Statements/for_each...in" title="en/JavaScript/Reference/Statements/for_each...in">for each...in</a> - similar to <code>for...in</code>, but iterates over the values of object's properties, rather than the property names themselves. (<a href="/en/JavaScript/New_in_JavaScript/1.6" title="en/New_in_JavaScript_1.6">New in JavaScript 1.6</a>.)</li> <li><a href="/en/JavaScript/Reference/Statements/for" title="en/Core_JavaScript_1.5_Reference/Statements/for">for</a></li> <li><a href="/en/JavaScript/Guide/Iterators_and_Generators" title="en/Core_JavaScript_1.5_Guide/Iterators_and_Generators">Generator expressions</a> (uses the <code>for...in</code> syntax)</li>
</ul>
<p>{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Sentencias/for...in", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/for...in", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/for...in", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/for...in" } ) }}</p>
Revert to this revision