JavaScript for/inStatement
JavaScript Statement Reference
Examples
Loop through object properties:
var person = {fname:"John", lname:"Doe", age:25};
var text = "";
var x;
for (x in person) {
text += person[x] + " ";
}
textOutput:
John Doe 25
Try it yourself »
Definition and Usage
The for/in statement loops through the properties of an object.
Each time the code in the loop executes, it performs an operation on an array element or an object property.
JavaScript supports different types of loops:
- for - loop a block of code a certain number of times
- for/in - loop through the properties of an object
- while - loop a specified block of code when a specified condition is true
- do/while- also loop a specified block of code when a specified condition is true
Note:Do not use the for/in statement to loop through array indices. You can use theforstatement instead.
Browser Support
| Statement | |||||
|---|---|---|---|---|---|
| for/in | Yes | Yes | Yes | Yes | Yes |
Syntax
for (var in
object) {
The block of code to be executed
}
The block of code to be executed
}
Parameter Values
| Parameter | Description |
|---|---|
| var | Required. The specified variable can be an array element or an object property. |
| object | Required. Specifies the object to iterate over. |
Technical Details
| JavaScript Version: | 1.0 |
|---|
Related Pages
JavaScript Tutorial:JavaScript For Loop
JavaScript Reference:JavaScript for Statement
JavaScript Statement Reference Other Extensions