Iterate with JavaScript Do...While Loops -- a simple example where this is useful

Tell us what’s happening:

Can someone please give an example as to where doing this might come handy?

var ourArray = []; 
var i = 5;
do {
} while (i < 5);

A do while loop is a ‘post-test’ loop meaning it will run at least once.

A simple use-case would be say you want to get input from a user it has to run at least once so you can test against their input in the condition statement after the while.

Here is a more extensive answer:

A Quora Do While Explanation

In the Quora answer one example used is an exit/continue prompt.

do the program while the user inputs continue if the user inputs exit or anything else the loop stops. You still need input from the user so it has to run at least once or more than once if the user chooses continue the next time the program gets to loop.

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.