Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript for/in Statement

JavaScript for/in Statement

JavaScript Statement Reference 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:

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 JavaScript statement reference manual