for...in

Table of contents

  1. 1. Summary
  2. 2. Syntax
  3. 3. Parameters
  4. 4. Description
  5. 5. Example
  6. 6. See also
Table of contents
  1. 1. Summary
  2. 2. Syntax
  3. 3. Parameters
  4. 4. Description
  5. 5. Example
  6. 6. See also

Summary

Iterates a specified variable over all the properties of an object, in arbitrary order. For each distinct property, the specified statement is executed.

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

Syntax

for (variable in object)
  statement

Parameters

variable
Variable to iterate over every property. This variable is accessible outside the loop after the loop completes. If the variable was not declared with the optional var keyword, it is accessible before the loop starts from where it is declared, potentially outside any containing function.
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

A for...in loop does not iterate over built-in properties. These include all built-in methods of objects, such as String's indexOf method or Object's toString method. However, the loop will iterate over all user-defined properties (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, the value exposed by the loop will be its value at that later time. A property which is deleted before it has been visited will not then 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 will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.

Although it may be tempting to use this as a way to iterate over an Array, this is a bad idea. The for...in statement iterates over user-defined properties in addition to the array elements, so if you modify the array's non-integer or non-positive properties (e.g. by adding a "foo" property to it or even by adding a method or property to Array.prototype), the for...in statement will return the name of your user-defined properties in addition to the numeric indexes. Also, because order of iteration is arbitrary, iterating over an array may not visit elements in numeric order. Thus it is better to use a traditional for loop with a numeric index when iterating over arrays. Similar arguments might be used against even using for...in at all (at least without propertyIsEnumerable() or hasOwnProperty() checks), since it will also iterate over Object.prototype (which, though usually discouraged, can, as in the case of Array.prototype, be usefully extended by the user where are no namespacing concerns caused by inclusion of other libraries which might not perform the above checks on such iterations and where they are aware of the effect such extension will have on their own use of iterators such as for...in).

Example

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

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

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

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

See also

Tags (0)

Attachments (0)

 

Attach file