Working with JavaScript Loops
This article was AI-drafted and has not been reviewed yet. Spotted a mistake? Tell us.
Learn how to use loops in JavaScript to execute a block of code repeatedly.
Introduction to JavaScript Loops
Loops are used to execute a block of code repeatedly.
Step 1: For Loop
The for loop is used to execute a block of code for a specified number of times.
for (let i = 0; i < 5; i++) {
console.log(i);
}
Step 2: While Loop
The while loop is used to execute a block of code as long as a condition is true.
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
Step 3: Do-While Loop
The do-while loop is used to execute a block of code at least once, and then repeat it as long as a condition is true.
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
By following these steps, you can learn how to use loops in JavaScript.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based javascript courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for javascript books — check the publication year so the examples match current versions.
Search Amazon →