Related Tutorials

Looping Statements

Statements

Description

for

Probably the most common looping
statement, "for" executes the enclosed statements
multiple times based on the 3 conditions an initial, limit, and
increment condition) defined inside the
for(...) parenthesis.

For/in is a special looping statement
that lets you loop through the properties of an object, either built
in or custom objects.

Syntax:

for (var prop in object)
statement

where "prop" is an arbitrary variable that will hold the
name of the current property of the object as it is being looped.
The value of the property is accessed using object[prop].
During each iteration, "prop" is overwritten with the
current property's name, and the body statement(s) of the
for/in loop executed.

For built in objects, for/in may not necessarily
loop through every property within it, but rather, only ones that
are enumerable. With custom objects, all of its properties are
enumerable btw.

Here are
two examples.

Example 1: This example writes out all the properties of
the "navigator" object, plus their values: