Act First, Ask Later

The do...while loop is the " rebel " among loops. It executes the action first, and only then checks if it was worth doing.

1. What's the difference?

In a regular while, the check happens before executing the body. If the condition is false immediately, the body won't execute even once. In do...while, the body will execute at least once.

2. When is this needed?

It is most often used for interacting with the user. For example, when you WANT a question to be asked at least one time:

let password;
do {
  password = prompt("Enter the password", "");
} while (password !== "123");
Note

In practice, this loop is used 10 times less often than while or for, but it's useful to know so you don't write redundant code before a loop.