Core JavaScript 1.5 Reference:Statements:for...in
From MDC
Contents |
[edit] 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 |
[edit] Syntax
for (variable in object) statement
for (var variable in object) statement
[edit] Parameters
-
variable - Variable to iterate over every property, optionally declared with the
varkeyword. This variable is local to the function, not to the loop. (In other words, when the loop completes the variable has the value of the last property visited.)
-
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.
[edit] 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. 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.
[edit] Examples
[edit] Example: Using for...in
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.
function show_props(obj, objName) {
var result = "";
for (var i in obj) {
result += objName + "." + i + " = " + obj[i] + "\n";
}
return result;
}
[edit] See also
- for each...in - similar to
for...in, but iterates over the values of object's properties, rather than the property names themselves. (New in JavaScript 1.6.) - for