Javascript - for in Statement


The for-in statement is an iterative statement. It is used to enumerate the properties of an object.

Here's the syntax:

for (property in expression)

And here's an example:

for (var propName in window) {

Here, the for-in statement is used to display all the properties of the BOM window object.

Each time through the loop, the propName variable is filled with the name of a property that exists on the window object.

This continues until all of the available properties have been enumerated over.

The var operator in the control statement is recommended for ensuring the use of a local variable.

Object properties in ECMAScript are unordered, so the order in which property names are returned in a for-in statement cannot be predicted.