JScript provides a special kind of loop for looping through all the user-defined properties of an object, all the elements of an array, or all the elements of a collection. The loop counter in a for...in loop is a string or object rather than a number. It contains the name of the current property, the index of the current array element, or the current element in the collection.
Using for...in Loops
The following code illustrates the use of the for...in construct.
В | ![]() |
---|---|
// Create an object with some properties. var prop, myObject = new Object(); myObject.name = "James"; myObject.age = 22; myObject.phone = "555 1234"; // Loop through all the properties in the object. for (prop in myObject){ print("myObject." + prop + " equals " + myObject[prop]); } |
The output of this program is:
В | ![]() |
---|---|
myObject.name equals James myObject.age equals 22 myObject.phone equals 555 1234 |
Note that the new behavior of the for...in loop construct in JScript eliminates the need to use the Enumerator object to iterate elements of a collection.
See Also
Reference
for...in StatementOther Resources
Loops in JScriptJScript Conditional Structures
JScript Reference