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 Review while loops, do...while loops, and Loop Conditions

Why would the while loop run the code if the condition is 'false'? I thought that the answer would be only 'Goodbye'?

while (false) {
    console.log('Hello');
}

console.log('Goodbye');```

1 Answer

Steven Parker
Steven Parker
230,995 Points

You are correct. Since the condition for the while loop is false, the loop never runs and 'Hello' never prints. But the second console.log does run because it's outside the loop.

Perhaps a stray mouse click caused a different answer to be selected? I've done that once or twice.

The answer to the question is actually Hello and Goodbye, and the explanation is: "Even though the condition is false, a do..while loop always run at least once."

Steven Parker
Steven Parker
230,995 Points

There are two similar questions. The code shown above is from the question on the plain "while" and the answer is just "Goodbye" for the reasons I shared.

There's also a question about a "do...while" that has different code and a different answer. Note that the quiz questions are randomized and you may not get both of them in the same quiz session.

It seems that both of those questions you mentioned have the wrong answers to them on the quiz

nvm, I think it was bugged or something because now it has the right answers, thank you so much for your help