JavaScript Loops

Complete guide to JavaScript loops: for, while, do-while, break and continue statements.

For Loop

for (var i = 0; i < 10; i++) {
    document.write(i + ": " + i*3 + "<br />");
}

var sum = 0;
for (var i = 0; i < a.length; i++) {
    sum += a[i];
}               # parsing an array

html = "";
for (var i of custOrder) {
    html += "<li>" + i + "</li>";
}

While Loop

var i = 1;                      # initialize
while (i < 100) {               # enters the cycle if statement is true
    i *= 2;                     # increment to avoid infinite loop
    document.write(i + ", ");   # output
}

Do While Loop

var i = 1;                      # initialize
do {                            # enters cycle at least once
    i *= 2;                     # increment to avoid infinite loop
    document.write(i + ", ");   # output
} while (i < 100)               # repeats cycle if statement is true

Break

for (var i = 0; i < 10; i++) {
    if (i == 5) { break; }          # stops and exits the cycle
    document.write(i + ", ");       # last output number is 4
}

Continue

for (var i = 0; i < 10; i++) {
    if (i == 5) { continue; }       # skips the rest of the cycle
    document.write(i + ", ");       # skips 5
}