JavaScript for/in Statement

Iterate (loop) over the properties of an object

Definition and Usage

The for...in statements combo iterates (loops) over the properties of an object.

The code block inside the loop is executed once for each property.

Note

Do not use for...in to iterate an array if the index order is important. Use a for loop instead.

See Also:

The JavaScript for...in Tutorial

Syntax

for (x in object) {
  code block to be executed
}

Parameters

Parameter Description
x Required.
A variable to iterate over the properties.
object Required.
The object to be iterated

JavaScript Loop Statements

StatementDescription
breakBreaks out of a loop
continueSkips a value in a loop
whileLoops a code block while a condition is true
do...whileLoops a code block once, and then while a condition is true
forLoops a code block while a condition is true
for...ofLoops the values of any iterable
for...inLoops the properties of an object

More Examples

Iterate over the properties of window.location:

let text = "";
for (let x in location) {
  text += x + "
";
}
document.getElementById("demo").innerHTML = text;

Browser Support

for...in is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes