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 trialBrandi Thompson
2,264 PointsWhy 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
Chyno Deluxe
16,936 PointsIf 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.
Adarsh Prabhu
15,394 PointsIt'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?
Chyno Deluxe
16,936 Pointscan you provide which example you are speaking of please.
Brandi Thompson
2,264 PointsBrandi Thompson
2,264 PointsThanks 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');
Chyno Deluxe
16,936 PointsChyno Deluxe
16,936 PointsCorrect, 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.