for Redirect 1

This feature has been removed from the Web. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.


Iterates a specified variable over all values of object's properties. For each distinct property, a specified statement is executed.

The for statement is deprecated as the part of ECMA-357 (E4X) standard. E4X support has been removed, but for will not be disabled and removed because of backward compatibility considerations. Consider using for...of instead. (Please refer to bugĀ 791343.)

Version Information

Implemented in: JavaScript 1.6
ECMAScript Version: none


for each (variable in object) {


Variable to iterate over property values, optionally declared with the var keyword. This variable is local to the function, not to the loop.
Object for which the properties are iterated.
A statement to execute for each property. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements.


Some built-in properties are not iterated over. These include all built-in methods of objects, e.g. String's indexOf method. However, all user-defined properties are iterated over.


Example: Using for

Warning: Never use a loop like this on arrays. Only use it on objects. Details.

The following snippet iterates over an object's properties, calculating their sum:

var sum = 0;
var obj = {prop1: 5, prop2: 13, prop3: 8};

for each (var item in obj) {
  sum += item;

print(sum); // prints "26", which is 5+13+8

See also

  • - a similar statement that iterates over the property names.
  • for...of - a similar statement that iterates over the property values but there is some behavior that is different from for statements.
  • for
  • Array comprehensions (these use for each)
  • Iterator - a function that takes a collection as an argument and allows traversal of its properties through subsequent methods

Document Tags and Contributors

 Last updated by: Sheppy,