English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript for...in statement

 JavaScript Statements and Variable Declarations

for...in The statement traverses the enumerable properties of an object.

The code block within the loop will execute once for each property.

JavaScript provides the following types of loops:

  • for - The loop traverses the code block several times

  • for...in - Traversing the properties of an object

  • while - The loop traverses the code block when the specified condition is true

  • do...while - The loop executes a block of code once and then repeats the loop when the specified condition is true

The for...in loop traverses the properties of an object in an arbitrary order.

Note: The for...in loop should not be used for iterating over Arrays that require index order. If you need to iterate, please useforStatement.

Syntax:

for (variable in object) { 
    //The statement to be executed
}
var myObj = {
name: "Seagull",
age:22,
height: 175,
city: "New Delhi",
    getNothing: function () { return ""; }
;
for (let x in myObj) {
    document.write(x);
}
Test and See‹/›

In each iteration, one of the Object's properties is assigned to a variable, and then the loop continues until all properties of the Object are processed.

Browser Compatibility

All browsers fully support the for ... in statement:

Statement
for...inIsIsIsIsIs

Parameter Value

ParameterDescription
variableEach iteration will assignvariableAssign a different property name
objectThe specified object to be iterated

Technical Details

JavaScript Version:ECMAScript 1

More Examples

The following example implementsfor ... inLoop and print the Web browser'sNavigatorObject:

for (let x in navigator) {
    document.write(x);
}
Test and See‹/›

Related References

JavaScript Tutorial:JavaScript for Loop

JavaScript Reference:JavaScript for Statement

 JavaScript Statements and Variable Declarations