Loops

Loops allow you to repeat steps until a specified condition is met. JavaScript has several different syntax for expressing the conditions of the loop, as well as explicit statements to jump out of the loop.

For Loop

A for loop repeats until a specified condition evaluates to false:

Listing 2–17. JavaScript for Loop

for(var i = 0; i < 10; i++) {   // do something 10 times }
While Loops

A while loop repeats until a specified condition evaluates to true:

Listing 2–18. JavaScript while Loop

i = 0; while( i < 5) {     i++; // do something 5 times }
Do/while

Similar to the while loop, this syntax places the logic to perform before the exit condition. You use this method to execute a loop before evaluating the condition, ensuring that the ...

Get Beginning Facebook Game Apps Development 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.