10.5 do Loop

As mentioned earlier, JavaScript has three types of loops—the while loop, do loop, and for loop. Next up—the do loop.

Syntax and Semantics

Note the do loop’s syntax template at the left of FIGURE 10.6. It shows the do loop’s condition at the bottom. That contrasts with the while loop, where the condition is at the top. Having the condition tested at the bottom guarantees that the do loop executes at least one time. After all, the condition is the only way to terminate the loop, and the JavaScript engine won’t check the condition (at the bottom) until after executing the lines above it. In the syntax template, note the semicolon at the right of the condition. That’s also different from the while loop. Finally, note that the ...

Get Web Programming with HTML5, CSS, and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.