JavaScript for/in Statement

Definition and Usage

The for/in statement loops through the properties of an object.

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

JavaScript supports different types of loops:

  • for - Loop through a code block multiple times
  • for/in - Loop through the properties of an object
  • for/of - Loop through the values of an iterable object
  • while - Loop through a code block while the specified condition is true
  • do/while - Execute a code block once, then repeat the loop if the specified condition is true

Note:Do not use the for/in statement to iterate over arrays where the index order is important. Use the for statement instead.

Example

Loop through the properties of an object:

var person = {fname:"Bill", lname:"Gates", age:25}; 
var text = "";
var x;
for (x in person) {
  text += person[x] + " ";
}

Try It Yourself

Syntax

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

Parameter Value

Parameter Description
var Required. The variable for iterating over object properties.
object Required. The specified object to be iterated.

Technical Details

JavaScript Version: ECMAScript 1

Browser Support

Statement Chrome IE Firefox Safari Opera
for/in Support Support Support Support Support

Related Pages

JavaScript Tutorial:JavaScript For Loop

JavaScript Reference Manual:JavaScript for Statement