STATEMENT:
for...in
for
(variable
in
object) {statements}
The for...in statement is used to iterate a declared variable
over every property in a specified object. The code in the body of the
for ... in loop is executed once for each property. The variable
argument can be a named variable, an array element, or a property of
the object.
This example simply displays the names of all the properties of the
'drink' object.
Code:
var i;
for(i in drink)
document.write(i + "<BR>");
You can also have a specified variable iterate over the values of
an object's properties by placing it between square brackets after the
object name. Expanding on the previous example, the following code displays
both the name of each property in the 'drink' object and its value:
Code:
var i;
for(i in drink)
document.write(i + ": " + drink[i]
+ "<BR>");
It is very easy to loop through an array. In this last example, starname is
the name of an array element in an array called, starchart.
Code:
for(starname in starchart)
document.write(starname + "<BR>");
|