Locked lesson.
About this lesson
Looping through things using While loops.
Exercise files
Download this lesson’s related exercise files.
While Loops.docx58.9 KB While Loops - Solution.docx
59.1 KB
Quick reference
While Loops
While loops are a great way to loop through conditions.
When to use
While loops are easier to use than For loops.
Instructions
The format of a while loop is:
while (condition) {
// do something
}
While loops keep looping while a condition is true. Be sure to set your counter variable before the loop, and increment or decrement the counter inside the loop!
Example:
var i = 1;
while (i < 10) {
document.write(i + "<br/>");
i++;
}
Hints & tips
- While loops keep looping while a condition is true.
- They're easier to create than For loops
- Watch out for infinite loops!
Lesson notes are only available for subscribers.