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 use false?

I get the while loop eg:

var i = 0;
while (i < 10) {
 console.log('i is less than 10');

but what i dont understand is when do you use

while(false) {
document.write('do this');
document.write('do that');

the "false" value confuses me because i don't understand what i am checking against

You did not read my question properly tracy, i understand the top part of the while..... I don,t get the while (false) part.... when will i ever use something like this? because the code in the block will never run.... and if set tot true it will become and infinite loop. but what are you comparing against to see whether is true or false ?

4 Answers

Hey Gavin,

I see your question clearly. The question in the quiz uses false as a means to see what you know of what happens in a do while loop. If you're unaware, you cannot use false in a standard while loop and expect any code to execute. The same actually goes for a do while loop, as well, but there is one key difference: do while loops always execute the code first before checking the conditional unlike standard while loops which first check the conditional and then execute the code if expression is true.

So, when this code comes up in the quiz:

do {
} while (false)

Since, this is a do while loop, the first console.log('Hello'); is executed. The do while checks the condition of the loop and sees that it is false, which means it exits the loop. Then it executes the second console.log: console.log('Goodbye');. So, the output is "Hello" and then "Goodbye".

Ah so Marcus if I'm understanding you correctly the program, the first time through a do/while loop assumes that the condition is false if it states so, and then on it's next run through it "realizes" that the do/while loop isn't false because the default condition is true? I hope I'm making sense...

Essentially, your counter will count up to 10. So what it does is check to see if it's at 10. If the counter is NOT at 10, then it is "false" so it will keep running. Once it reaches 10, then it's true and the condition is changed, and the code stops executing.

I hope this helps!

Oops... sorry about that. You'd use it IF you had a parameter that was set to false initially -- to repeat over and over until the condition was true. Once that condition changes, then the code does not run... The code ONLY runs as long as the condition is false. Essentially you're checking to see if whatever condition you set is false -- some values / variables are initially set to false.

A good example is a form. If you needed to make sure that you couldn't submit a form unless certain fields were filled in, then you could set this up like so:

while(false) {
  // code that stops the field from submitting if certain fields are empty, etc.

Once the user has filled in all the required form fields, then the condition is true... (because the form fields aren't empty)... and the code in that while loop doesn't run.

Let me know if I still didn't understand your question..



While, and do while loops, cannot operate with a while (false) directive. The conditional within the loop must be true in order for the code to execute. Using false (or another falsey value) as a value in the while loop will cause the while loop to never execute.

Thanks, Marcus. I always mix that up myself. Apparently I need to revisit that video or something as I totally misunderstood his question, and then wasn't thinking about while loops properly as they aren't the type of loop that I'd use much. sigh And here I thought I was getting a good handle on JavaScript.

Whenever I'm about to explain anything, if it's even remotely foggy to me, I turn to my web console in Chrome (or in Firefox if you prefer) and write up a quick piece of code that pairs into what I'm going to be talking about. I've gained my familiarity with JavaScript by using the web console to its fullest advantage. It is an incredibly powerful tool!

Gertrude Dawson
Gertrude Dawson
5,371 Points

Thank you Marcus Parsons. I have been typing the codes I want tested in Workspaces but using the Chrome console is so much more efficient that that.