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 trialfrancisobrien
2,131 Pointswhile (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!
6 Answers
francisobrien
2,131 PointsI see, thanks Robert!
Robert Darby
9,360 PointsNot 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
}
francisobrien
2,131 PointsInteresting, 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!
Robert Darby
9,360 PointsA 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.
francisobrien
2,131 PointsThanks
francisobrien
2,131 PointsWhy 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.
Xavier Ritch
11,099 PointsThe 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
Shung Chen
6,526 Pointsboolean 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.
Robert Darby
9,360 PointsRobert Darby
9,360 PointsThe 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.