2: While Syntax


#1


The task was to complete the condition. WHY is the condition 'understand'?? (i found it on google) I don't even know what function this is performing! So short and simple looking but what the heck does var understand mean?! I must be overthinking this!

thank you! :slight_smile:


var understand = true;

while(**understand**){
	console.log("I'm learning while loops!");
	understand = false;
}


#2

The conditional in a while or if statement always renders as a boolean for final analysis. Whatever expression we write inside the parens will be cast to boolean and evaluated by true or false.

var understand = true;

while (understand) { }

In the above, understand is a boolean, so walks right on through (owing that it is true). We never need to (nor should) compare a boolean to a boolean.

while (understand === true)    // incorrect or poor form

while (understand)             // true to form

An expression that once cast, yields false will never be allowed past.

var understand = "This" && 0

console.log(understand)    // 0

Now when this value gets evaluated in the while (condtion) it will yield, false and the loop will not run.

0 converts to `false`

Getting the hang of it?


#3

So, is the output supposed to look like this?:

I'm learning while loops!
false


#4

The output will be the line that goes to the console inside the loop. That you are setting understand to false after logging is the reason the console displays it afterward. That's normal console behavior that can be ignored. Or you could set understand before logging and then it won't dispaly at the end.


#6