Introduction to JavaScript Loops: For, While, and Do/While
Learn how to use loops in JavaScript to repeat a block of code.
Introduction to JavaScript Loops
Loops are used to repeat a block of code for a specified number of times. In this tutorial, we will cover the basics of JavaScript loops.
For Loops
The for loop is used to repeat a block of code for a specified number of times. Here is an example of how to use a for loop:
for (let i = 0; i < 5; i++) {
console.log(i);
}
While Loops
The while loop is used to repeat a block of code while a certain condition is true. Here is an example of how to use a while loop:
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
Do/While Loops
The do/while loop is used to repeat a block of code while a certain condition is true. Here is an example of how to use a do/while loop:
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
Conclusion
In this tutorial, we covered the basics of JavaScript loops. We learned how to use for loops, while loops, and do/while loops to repeat a block of code.
Ready for more? These paid resources pick up where this lesson leaves off.
Project-based javascript video courses — the perfect paid next step after these free lessons.
Browse on Udemy →Hand-picked javascript books to master the fundamentals offline.
See on Amazon →Some links on this page are affiliate links: we may earn a commission at no extra cost to you. We only recommend tools we believe are genuinely useful.