while Loops

One kind of loop is called a while loop. In plain English, this translates to "While such-and-such is true, do the following." Figure 8-7 shows a while loop that prints the word happy three times.

Printing the word happy three times with a while loop

Figure 8-7. Printing the word happy three times with a while loop

Loops are a very common programming technique. They may seem strange the first couple of times you see them, but they are so common that after a while you'll understand them on sight.

The typical while loop starts with a variable set to zero, as in ❶ of Figure 8-7. The variable index is just like any other variable.

Once you've set this variable, the while loop begins. Line ❷ reads, ...

Get The Book of JavaScript, 2nd Edition 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.