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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.