Skip to main content

JavaScript While And Do Loop

Published 1 min readMarkdown
JavaScript While And Do Loop

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

About the author

  • I'm a full-stack web developer who loves building new projects and learning new things

    It might not show from my posts, but I'm also quite funny.

No comments yet. Be the first to comment.

More from this blog