This documentation is archived and is not being maintained.

for...in Statement

Executes one or more statements for each property of an object, or each element of an array or collection.

for ( [var] variable in {object | array | collection})
   statement

Arguments

variable
Required. A variable that can be any property name of object, any index of array, or any element of collection.
object
A JScript object over which to iterate.
array
An array over which to iterate. Can be a JScript Array object or a .NET Framework array.
collection
A collection over which to iterate. Can be any class that implements the IEnumerable or IEnumerator interfaces from the .NET Framework.
statement
Optional. Statements to be executed for each property of object or each element of array or collection. Can be a compound statement.

Remarks

Before each iteration of a loop, variable is assigned the next property name of object, the next index of array, or the next element of collection. You can use variable in any of the statements inside the loop to reference 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 member names of the object are assigned to variable. The for...in statement cannot loop over the members of non-JScript objects, such as .NET Framework objects.

Arrays are iterated in element order, starting with the smallest index and ending with the largest index. Because JScript Array objects can be sparse, the for...in statement accesses only the defined elements of the array. JScript Array objects may also have expando properties, in which case variable is assigned array indexes as property names. If the array is a multidimensional .NET Framework array, only the first dimension is enumerated.

For iteration over a collection, the elements are assigned to variable in the order in which they appear in the collection.

Example 1

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

function ForInDemo1() {
   var ret = "";

   // Initialize the object with properties and values.
   var obj : Object = {"a" : "Athens" , 
                       "b" : "Belgrade", 
                       "c" : "Cairo"};

   // Iterate over the properties.
   for (var key in obj)
      // Loop and assign 'a', 'b', and 'c' to key.
      ret += key + ":\t" + obj[key] + "\n";

   return(ret);
} // ForInDemo1

This function returns the string that contains:

a:      Athens
b:      Belgrade
c:      Cairo

Example 2

This example illustrates the use of the for ... in statement with a JScript Array object which has expando properties.

function ForInDemo2() {
   var ret = "";

   // Initialize the array.
   var arr : Array = new Array("zero","one","two");
   // Add a few expando properties to the array.
   arr["orange"] = "fruit";
   arr["carrot"] = "vegetable";

   // Iterate over the properties and elements.
   for (var key in arr)
      // Loop and assign 0, 1, 2, 'orange', and 'carrot' to key.
      ret += key + ":\t" + arr[key] + "\n";

   return(ret);
} // ForInDemo2

This function returns the string that contains:

0:      zero
1:      one
2:      two
orange: fruit
carrot: vegetable

Example 3

The following example illustrates the use of the for ... in statement with a collection. Here, the GetEnumerator method of the System.String object provides a collection of the characters in the string.

function ForInDemo3() {
   var ret = "";

   // Initialize collection.
   var str : System.String = "Test.";
   var chars : System.CharEnumerator = str.GetEnumerator();

   // Iterate over the collection elements.
   var i : int = 0;
   for (var elem in chars) {
      // Loop and assign 'T', 'e', 's', 't', and '.' to elem.
      ret += i + ":\t" + elem + "\n";
      i++;
   }

   return(ret);
} // ForInDemo3

This function returns the string that contains:

0:      T
1:      e
2:      s
3:      t
4:      .

Requirements

Version 5

Note   Looping over collections requires Version .NET.

See Also

for Statement | while Statement | JScript Arrays | String.GetEnumerator Method

Show: