JavaScript While And Do Loop

JavaScript While And Do Loop

·

1 min read

In this post, we will learn about JavaScript while loops and do while loops.

While Loop

The while loop keeps executing the code block until a specific condition is no longer true. And while the condition is true, it will keep executing the particular block of code.

JavaScript Code

var i = 0;
while(i<100)
{
document.write(i);
i++;
}

Image description

Requirements A while loop should have a counter variable inside, as well as a way of increasing or decreasing the counter. Otherwise, your loop will keep repeating forever without a way it can end.

Do while loop While at first confusing, the do-while loop is almost identical to the while loop. The only difference is that do while will execute once before even checking the condition.

In a do-while loop, even if the condition is not a met at all, the loop will execute once.

JavaScript Code

var i = 1110;
do
{
document.write(i);
i++;
}
while(i<100);

Image description