Main Page

while

while
The
while
statement is a pretest loop. This means the evaluation of the escape condition is done before
the code inside the loop has been executed. Because of this, it is possible that the body of the loop is
never executed. Syntax:
while(
expression
)
statement
For example:
var i = 0;
while (i < 10) {
i += 2;
}
for
The
for
statement is also a pretest loop with the added capabilities of variable initialization before
entering the loop and defining postloop code to be entered. Syntax:
for (
initialization
;
expression
;
post-loop-expression
)
statement
For example:
for (var i=0; i < iCount; i++){
alert(i);
}
This code defines a variable
i
that begins with the value
0
. The
for
loop is entered only if the conditional
expression (
i < iCount
) evaluates to
true
, making it possible that the body of the code might not be
executed. If the body is executed, the postloop expression is also executed, iterating the variable
i
.
for-in
The
for-in
statement is a strict iterative statement. It is used to enumerate the properties of an object.
Syntax:
for (
property
in
expression
)
statement
For example:
for (sProp in window) {
alert(sProp);
}
Here, the
for-in
statement is used to display all the properties of the BOM
window
object. The method
propertyIsEnumerable()
, discussed earlier, is included in ECMAScript specifically to indicate whether
or not a property can be accessed using the
for-in
statement.
55
ECMAScript Basics
05_579088 ch02.qxd 3/28/05 11:35 AM Page 55


JavaScript EditorFree JavaScript Editor     Ajax Editor


©