O'Reilly logo

The Book of JavaScript, 2nd Edition by thau!

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required