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

Brandi Thompson
Brandi Thompson
2,264 Points

Why does the code while (false) work?

I thought you had to state what is false e.g. ( treehouse === false ) assuming we had declared a variable called treehouse. Why doesn't this cause an error?

2 Answers

If you are referring to a do...while loop, the script inside the do{} will always run at least once. After it has completed all task inside the do, it will then run a check to see if the while's condition is true. If it is true, it will run the do task's again until the while condition is false.

I hope this helps.

Brandi Thompson
Brandi Thompson
2,264 Points

Thanks Chyno! I think I just got it: so the code while (false) is saying that the while condition itself is false.

I thought the quiz question I was looking at would be displayed with my question, but now I see that it isn't. To clarify, here was the code:

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

Correct, BUT, just to clarify, a while loop will NEVER run if the condition is false. It will only run if its condition is true.

A do...while loop how ever will always run once, then check the while loops condition to see if it is true. If so, it will run the do tasks until the condition is false.

Adarsh Prabhu
Adarsh Prabhu
15,394 Points

It's not clear clear to me where the condition is being tested. When the DO runs once, how does it get evaluated to the false condition?

can you provide which example you are speaking of please.