Skip to main content

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.


let i = 0;

while (i < 5) {
console.log(`Iteration ${i + 1}`);

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


Understanding and using the while loop is essential for performing repetitive tasks in your JavaScript programs.