for...in Statement

Microsoft Office JScript

Microsoft® JScript® for...in Statement
 Language Reference 
Version 5 

See Also


Description
Executes one or more statements for each property of an object, or each element of an array.
Syntax
for (variable in [object | array])
    statement

The for statement syntax has these parts:

Part Description
variable A variable that can be any property of object or any element of array.
object, array An object or array over which to iterate.
statement The statement or statements to be executed for each property of object or each element of array. Can be a compound statement.

Remarks
Before each iteration of a loop, variable is assigned the next property of object or the next element of array. You can then use it in any of the statements inside the loop, exactly as if you were using the property of object or the element of array.

When iterating over an object, there is no way to determine or control the order in which the members of the object are assigned to variable. Iterating through an array will be performed in element order, that is, 0, 1, 2, ...

The following example illustrates the use of the for ... in statement with an object used as an associative array:

function ForInDemo()
{
  // Create some variables.
  var a, key, s = "";
  // Initialize object.
  a = {"a" : "Athens" , "b" : "Belgrade", "c" : "Cairo"}
  // Iterate the properties.
  for (key in a)
  {
     s += a[key] + "<BR;>";
  }
  return(s);
}

Note  Use the enumerator object to iterate members of a collection.