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

2 Answers

andren
andren
28,558 Points

A while loop will run as long as the condition you provide it evaluates to the Boolean value true. Comparisons and other conditions like that actually evaluate to true and false based on the result of the comparison.

In the video the Boolean value true is entered directly as the condition, meaning that the loop will always run since there is no situation where true does not evaluate to true. It's basically like providing a condition of 1 == 1, it's a condition that will never be false.

It's worth nothing though that creating a loop with true as its condition is not always a bad idea. In fact its a pretty common practice when you want the loop to run an uncertain number of times, but when you do that you have to have a break statement somewhere within the loop. break will cause the while loop to end regardless of what its condition evaluates to.

Learning coding
seal-mask
.a{fill-rule:evenodd;}techdegree
Learning coding
Front End Web Development Techdegree Student 9,937 Points

So in the video the word true is not a boolean, but a condition? That seems confusing since true is used mostly as a boolean. And what is the point of a condition that has no evaluation? Or is that what you are explaining in the third paragraph that even though the condition is not evaluated it is possible to break out the loop to prevent an endless loop. And can you also see the statement break as an evaluation? Thanks.

andren
andren
28,558 Points

No, it is a Boolean, but it is provided as the condition of the loop. The condition of a while loop is just a value that has to ultimately evaluate to either true or false.

As I mentioned above all comparisons provided to a while loop is ultimately evaluated to a Boolean value anyway. For example 2 > 1 evaluates to true, 5 < 1 evaluates to false and so on. It is those evaluations that the while statement is actually looking at, it never really looks at the actual comparison, only the Boolean it results in.

By supplying a Boolean directly you are just cutting out the step of actually evaluating a comparison. As far as a the while loop is concerned a condition of true and a condition of 1 == 1 is the same thing, they will both ultimately result in true every time they are evaluated.

And the point is indeed what I tried to explain in the third paragraph, by using true as a condition you do essentially end up with an infinite loop, but by using break you can end the loop even though the condition to run it technically never evaluates to false which is normally what ends the loop.

That is most commonly done in situations where you don't really control when a loop will end, for example in a situation where you are using a while loop to continually prompt a user for some input where you only want to stop once the user has performed a certain action. In that case you could have an if statement inside the while loop that checked if the action had actually been completed and break if it had.