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 Exiting Loops

while (true) - I don't get using a boolean as a condition... confused!

while (true) { // this is an endless loop break; // but break, lets you "break out" of the loop }

I don't get how 'true' can be used as a condition above. What condition is being evaluated here? How can 'true' even be a condition? Confusion...

Thanks in advance!

The condition always needs to equal either true or false but usually you would do some kind of comparison to return true or false.

while( "Yes" === "Yes" ) is true so it runs while( "No" === "Yes" ) is false so it doesn't run.

This is just skipping the comparison and setting it directly too true. It's a static value and not a variable so it will always be true, hence the loop will never end.

6 Answers

I see, thanks Robert!

Not a problem at all.

You can also do things like this

let var = true;

while( var ) {
// do things here
var = false; // this will set the value of var to false and stop the loop
}

Interesting, thanks Robert. Follow up question: What is the advantage of using a while loop with a boolean, for example:

while (true) { // do stuff here if (condition) { break; } }

...over using a a do-while loop?

do { // Do stuff here } while (condition)

Seems like kind of the same thing... or am I missing something?

Thanks!

A do while loop will run the code block once before checking the condition, thats why the condition is at the bottom. A while loop will check the condition before running the code block, thats why the condition is at the top.

It's very common to need one or the other in the real world.

Hope that makes sense.

Thanks

Why won't the following code loop???

let shoppingList = [ 'eggs', 'milk'];

while (true) {
    addItem = prompt('Type to enter an item in your shopping list. Type quit to exit.');
  shoppingList.push( addItem );
    if ( addItem  === 'quit' ) {
    list.pop();
    break;
    }
}

document.write( shoppingList );

After adding an item to the list using prompt(), shouldn't it continue to 'loop' and continually re-prompt until the user types 'quit'? Instead it actually writes the array to the page... I can't understand why it breaks out of the loop.

The loop seems to work just fine, and typing "quit" in the dialog does successfully end the loop. However, the list keyword you used and called .pop() on, was previously undefined

boolean value = True or false

the if function will keep running as long as the condition is true

writing if (true) means the code will be executed indefinitely as nothing stop the condition as being true, unless we put a break on it.