The for each...in
statement is deprecated as the part of ECMA-357 (E4X) standard. E4X support has been removed, but for each...in will not be disabled and removed because of backward compatibility considerations. Consider using for...of instead. (Please refer to .)
The for each...in
statement iterates a specified variable over all values of object's properties. For each distinct property, a specified statement is executed.
for each (variable in object) {
statement
}
variable
var
keyword. This variable is local to the function, not to the loop.object
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.
for each...in
Warning: Never use a loop like this on arrays. Only use it on objects. See for...in
for more 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; } console.log(sum); // logs "26", which is 5+13+8
[Not part of a current ECMA-262 specification. Implemented in JavaScript 1.6 and deprecated.]
Created by Mozilla Contributors, license: CC-BY-SA 2.5