THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript for/in Statement

JavaScript Statements Reference JavaScript Statements Reference

Example

Loop through the properties of an object:

var person = {fname:"John", lname:"Doe", age:25};

var text = "";
var x;
for (x in person) {
    text += person[x];
}

The result of text will be:

John Doe 25
Try it yourself »

Definition and Usage

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

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

JavaScript supports different kinds of loops:

  • for - loops through a block of code a number of times
  • for/in - loops through the properties of an object
  • while - loops through a block of code while a specified condition is true
  • do/while - loops through a block of code once, and then repeats the loop while a specified condition is true

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


Browser Support

Statement
for/in Yes Yes Yes Yes Yes

Syntax

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

Parameter Values

Parameter Description
var Required. A variable that iterates over the properties of an object
object Required. The specified object that will be iterated

Technical Details

JavaScript Version: 1.0

Related Pages

JavaScript Tutorial: JavaScript For Loop

JavaScript Reference: JavaScript for Statement


JavaScript Statements Reference JavaScript Statements Reference