March 2012
Beginner
432 pages
10h 15m
English
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.
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
}
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
}
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 ...
Read now
Unlock full access