Loops

While loop  

while (expr) {
statements;
}

Do ... while

do {
  statements;
} while (expr);

For loop

for (initial-expr; cond-expr; incr-expr) {
  statements;
}

For ... in

for (var property-name in object-name) {
  statements;
}
  • Iterates through the properties of an object

Examples

for (var propName in myObject) {
  propertyValue = myObject[propName];
}

Jump statements

  • break : Break from the loop immediately.
  • continue : Continue on to the next iteration of the loop.
  • break LABEL : Jump to the statement immediately after the labeled statement (terminate the labeled statement).
  • continue LABEL : Jump to the labeled statement (restart a labeled statement or continue execution of a labeled loop)

Examples

var sum = 0;
var i = 1;
while (i < 10) {
    if (i == 3) {
        continue;  // Skip the rest of this loop iteration.
    }
    sum += i;

    if (sum > 15) {
        break;  // Exit the loop.
    }
}

Labels

  • Consists of an identifier followed by a colon
  • Used to identify the loop that the loop control statements refer to
  • If the label is omitted, the loop control statements refer to the innermost enclosing loop.

Examples

  • LABEL1: statement;
  • LABEL2: { statements; }