for( ... in ... ) ... (Iterator)

An iterator mechanism - a loop construct.

Availability:

ECMAScript edition - 2
JavaScript - 1.0
JScript - 5.0
Internet Explorer - 5.0
Netscape - 2.0
Netscape Enterprise Server - 2.0
Opera - 3.0
JavaScript syntax:-aLabel: for(anLValue in anObject) { someCode };
Argument list:aLabelAn optional label to identify the loop
anLValueA value that can be assigned to
anObjectAn object whose properties will be cycled
someCodeSome code that is executed each time round the loop

A for... in iteration statement is used to enumerate through the properties of an object.

The first item in the control construct is a container that a value can be assigned to. An LValue or Left-Hand Side expression in other words. The item following the in keyword, is the object whose properties are to be enumerated.

Each time round the loop, the name of an object property will be assigned to the LValue and it can then be used as an index to the property value in that object.

Properties that have the DontEnum attribute set will not be enumerated in this iteration statement.

During each iteration, the property name can be used as an array index key to extract the property value.

At version 1.2 of JavaScript, a named continue can be used with this iterator. If the named continue is executed, control passes to the top of the named for loop. The loop starts over with the next property name being assigned to the specified variable.

Warnings:

Example code:

   // Loop through the properties of an object only printing properties

   // that have the string data type.

   for(myProperty in myObject)

   {

      if(typeof(myObject[myProperty]) == "string")

      {

         document.write(myProperty, myObject[myProperty]);

      }

   }

See also:break, Compound statement, continue, do ... while( ... ), DontEnumerate, Flow control, for( ... ) ..., Host object, Iteration statement, Label, while( ... ) ...

Cross-references:

ECMA 262 edition 2 - section - 11.1.2

ECMA 262 edition 2 - section - 12.6.3

ECMA 262 edition 2 - section - 12.7

ECMA 262 edition 2 - section - 12.8

ECMA 262 edition 3 - section - 12.6.4

ECMA 262 edition 3 - section - 12.7

ECMA 262 edition 3 - section - 12.8

Wrox Instant JavaScript - page - 34