while (expr) {
... code executed while expr is true ...
}
do {
... code executed once and then while expr is true ...
} while (expr);
for (initexpr; testexpr; modexpr) {
... code executed while testexpr is true ...
}
for (variable in object) {
... code executed once for each element in object ...
}
break and continue
break lets you immediately exit a loop
continue skips the remaining statements in a loop and starts the next iteration
you have to be careful when using continue in a while
or do/while loop since you might be skipping your loop
counter modification statement which is usually the last
statement in the loop
for (j=1; j<11; j++) {
if (j==2) continue;
if (j==5) break;
document.writeln(j + '<br />');
}
labeled break and continue
labeled break jumps to the end of the labeled statement
continue terminates the current iteration of the labeled loop and
starts the next iteration
you have to be careful when using continue in a while
or do/while loop since you might be skipping a loop
counter modification statement which is usually the last
statement in the loop
outerloop:
for (i=0; i<5; i++) {
innerloop:
for (j=0; j<5; j++) {
if (i==3) continue outerloop
if (j==2) break innerloop;
document.writeln(i + ', ' + j + '<br />');
}
}
void operator
void evaluates the operand that follows, throws away the value, and returns undefined
this may be used to generate the undefined value in older browsers
often used to evaluate an expression without generating a result
the example uses it in the JavaScript code for an href attribute to prevent
the original window from being trashed when attempting to open a new window
Demonstration of more intelligent sorting (This code
sorts numerically if all elements are numbers, otherwise sorts alphabetically. It
also uses the built-in array sort method.)