for...in

  • Revision slug: JavaScript/Reference/Statements/for...in
  • Revision title: for...in
  • Revision id: 411365
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment CleanUp / MarkUp

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
ECMA Version: ECMA-262

Syntax

for (variable in object) {
  ...
}

Parameters

variable
A different property name is assigned to variable on each iteration.
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.prototype 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 itself and those the object inherits from its constructor's prototype (properties closer to the object in the prototype chain override prototypes' 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.

If you only want to consider properties attached to the object itself, and not its prototypes, use getOwnPropertyNames or perform a hasOwnProperty check (propertyIsEnumerable can also be used). Alternatively, if you know there won't be any outside code interference, you can extend built-in prototypes with a check method.

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 (or Array.forEach or the non-standard for...of loop) when iterating over arrays where the order of access is important.

Examples

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 */ 

The following function illustrates the use of hasOwnProperty: the inherited properties are not displayed.

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

function ColoredTriangle() {
  this.color = "red";
}

ColoredTriangle.prototype = triangle;

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

o = new ColoredTriangle();
alert(show_own_props(o, "o")); /* alerts: o.color = red */ 

Iterating over only

{{todo}}

See also

Revision Source

<h2 id="Summary" 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="standard-table">
  <thead>
    <tr>
      <th colspan="2">Statement</th>
    </tr>
  <tbody>
    <tr>
      <td>Implemented in:</td>
      <td>JavaScript 1.0</td>
    </tr>
    <tr>
      <td>ECMA Version:</td>
      <td>ECMA-262</td>
    </tr>
  </tbody>
</table>


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

<pre class="syntaxbox">
for (<var>variable</var> in <var>object</var>) {
  <i>...</i>
}</pre>



<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt><code>variable</code></dt>
  <dd>A different property name is assigned to <em>variable</em> on each iteration.</dd>
  <dt><code>object</code></dt>
  <dd>Object whose enumerable properties are iterated.</dd>
</dl>




<h2 id="Description" name="Description">Description</h2>
<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.prototype</code> that are not enumerable, such as <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/String">String</a></code>'s <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/String/indexOf">indexOf</a></code> method or <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object">Object</a></code>'s <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/toString">toString</a></code> method. The loop will iterate over all enumerable properties of the object itself and those the object inherits from its constructor's prototype (properties closer to the object in the prototype chain override prototypes' 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-US/docs/JavaScript/Reference/Operators/delete#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" id="hasOwnPropertyNote">
  <p>If you only want to consider properties attached to the object itself, and not its prototypes, use <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a> or perform a <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty">hasOwnProperty</a> check (<a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a> can also be used). Alternatively, if you know there won't be any outside code interference, you can extend built-in prototypes with a check method.</p>
</div>
<div class="note" id="arrayNote">
  <p><strong><code>for..in</code> should not be used to iterate over an <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array">Array</a> where index order is important.</strong> 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-US/docs/JavaScript/Reference/Statements/for">for</a> loop with a numeric index (or <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach">Array.forEach</a> or the non-standard <code><a href="/en-US/docs/JavaScript/Reference/Statements/for...of">for...of</a></code> loop) when iterating over arrays where the order of access is important.</p>
</div>
<h2 id="Example" name="Example">Examples</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>
<p>The following function illustrates the use of hasOwnProperty: the inherited properties are not displayed.</p>
<pre class="brush: js">
var triangle = {a:1, b:2, c:3};

function ColoredTriangle() {
  this.color = "red";
}

ColoredTriangle.prototype = triangle;

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

o = new ColoredTriangle();
alert(show_own_props(o, "o")); /* alerts: o.color = red */ 
</pre>

<h3 id="Iterating_over_only">Iterating over only</h3>
<p>{{todo}}</p>

<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Statements/for...of"><code>for...of</code></a> - a similar statement that iterates over the property <em>values</em></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Statements/for_each...in"><code>for each...in</code></a> - a similar statement, but iterates over the values of object's properties, rather than the property names themselves (<a href="/en-US/docs/JavaScript/New_in_JavaScript/1.6">New in JavaScript 1.6</a> but deprecated)</li>
  <li><a href="/en-US/docs/JavaScript/Reference/Statements/for">for</a></li>
  <li><a href="/en-US/docs/JavaScript/Guide/Iterators_and_Generators">Generator expressions</a> (uses the <code>for...in</code> syntax)</li>
  <li><a href="/en-US/docs/Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames"><code>getOwnPropertyNames</code></a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty"><code>hasOwnProperty</code></a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach"><code>Array.prototype.forEach</code></a></li>
</ul>
Revert to this revision