JavaScript while
Loop (Live Playground)
In this tutorial, we will explore the while
loop, and learn how to use it in our code to help perform repetitive tasks.
while
Loop
A while
loop is used to execute a block of code as long as a specified condition is true
.
Example:
let i = 0;
while (i < 5) {
console.log(`Iteration ${i + 1}`);
i++;
}
In the example above, the loop will run as long as i
is less than 5, and the output will display the iteration number from 1 to 5.
Live Playground, Try it Yourself
Conclusion
Understanding and using the while
loop is essential for performing repetitive tasks in your JavaScript programs.