English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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.
All browsers fully support the for ... in statement:
Statement | |||||
for...in | Is | Is | Is | Is | Is |
Parameter | Description |
---|---|
variable | Each iteration will assignvariableAssign a different property name |
object | The specified object to be iterated |
JavaScript Version: | ECMAScript 1 |
---|
The following example implementsfor ... inLoop and print the Web browser'sNavigatorObject:
for (let x in navigator) { document.write(x); }Test and See‹/›
JavaScript Tutorial:JavaScript for Loop
JavaScript Reference:JavaScript for Statement