JavaScript for/in Statement
JavaScript statement reference manual
Example
Loop through object properties:
var
person
=
{
fname
:
"
John
"
,
lname
:
"
Doe
"
,
age
:
25
}
;
var
text
=
"
"
;
var
x
;
for
(
x
in
person
)
{
text
+=
person
[
x
]
"
"
;
}
text The output result is:
John Doe 25
Try it »
Definition and Usage
The for/in statement is used to loop through object properties.
Each time the code in the loop runs, it operates on one element of an array or one property of an object.
JavaScript supports different types of loops:
- **for **- Loops a code block a certain number of times
- **for/in **- A loop iterates over the properties of an object
- **while **- Loops a specified code block while a specified condition is true
- do/while - Also loops a specified code block while the specified condition is true
Note: Do not use the for/in statement to loop through array indexes; you can use the for statement instead.
Browser support
| Statement | |||||
|---|---|---|---|---|---|
| for/in | Yes | Yes | Yes | Yes | Yes |
Syntax
for (var
in
object) {
The code block to be executed
}
Parameter value
| Parameters | Description |
|---|---|
| var | Required. The specified variable can be either an array element or a property of an object. |
| object | Required. Specifies the object to be iterated. |
Technical Details
| JavaScript version: | 1.0 |
|---|
Related pages
JavaScript tutorial: JavaScript For Loop
JavaScript Reference Manual: JavaScript for Statement
JavaScript statement reference manual