JavaScript Editor Ajax toolkit     Ajax website 

Main Page

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 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 Loops

The following code illustrates the use of the construct.

В CopyCode imageCopy Code
// Create an object with some properties.
var prop, myObject = new Object(); = "James";
myObject.age = 22; = "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:

В CopyCode imageCopy Code equals James
myObject.age equals 22 equals 555 1234

Note that the new behavior of the loop construct in JScript eliminates the need to use the Enumerator object to iterate elements of a collection.

See Also

JavaScript Editor Ajax toolkit     Ajax website