Do…While loops
Coding with JavaScript
🕑 This lesson will take about 10 minutes
Unlike the regular while loop, a do..while loop will repeat the block of code in the loop first, and then check if the condition evaluates to true before it runs again (rather than testing the condition before running code in the loop). A do..while loop will always execute the instructions inside the loop at least once, even if the condition evaluates to false. This is an example of what is called a post-test repetition structure. On the other hand, a regular while loop (that only runs a block of code after a condition evaluates to true) is an example of a pre-test repetition structure.
Watch the video below, then scroll down the page to see the sample code explained.
Sample HTML and JavaScript code
Next tutorial: For loops