Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

Asad Sajon
PLUS
Asad Sajon
Courses Plus Student 918 Points

Don't understand do...while loop

Hi,i'm feeling pained.Don't understand the 'do..while' loop.I have learned 'While' and 'For' loop with so much interest.But don't understand the 'do...while' loop.Really feeling disappointed.Anyone can help me?

3 Answers

Samuel Webb
Samuel Webb
25,370 Points

With a while loop, it could potentially never run. If the condition is false, you will never run the code inside.

while (false) {
    console.log("I will not run since my condition is false.");
}

The only difference with a do...while loop is that no matter what, it will run the first time. The reason for this is that, unlike the while loop, it doesn't evaluate the condition until after the first iteration of the loop has run.

do {
    console.log("I'll run once even though my condition is false;");
} while (false);

This is the only difference between a while and a do...while loop. If you understand a while loop, you pretty much understand a do...while loop.

Hopefully this clears things up for you.

Samuel Webb
Samuel Webb
25,370 Points

No problem. Glad to help.