Can someone explain WHY this while loop works?


So here is the code to the problem:

understand = true;

console.log("I'm learning while loops!");
//Change the value of 'understand' here!

I changed understand to false as the instructions recommended and it passed the lesson the first time around. However, I'm having trouble understanding why it works the way that it does.

In the while(understand) part, why is understand assumed to be true? It doesn't say while(understand=1) or while(understand=true) so why doesn't it keep looping after understand=false in the curly brackets below? The way that I see it, at the end of the first loop, understand becomes false, then enters the loop again and launches understand a second time. Now, understand is false, but the condition doesn't keep it from being false, so why doesn't the loop continue?

If someone could clear this up for me, I would appreciate it. Thanks!


In one of the next problems, I am encountering this same problem of understanding.

var uh = true

while(uh=true) {
var uh = false;

for(i = 0; i < 1; i++) {

When while(uh=true), the page crashes when run. However, when it's written as while(uh), it closes off after the loop. So confused......


Ok important things first:
Never and I really mean NEVER try a condition like this:


or this


The problem is that = is not a comparison operator but an assignment operator so you don't check if understand is 1 or true but you make it become 1 or true regardless of what it previously was. And if you try for example console.log(variable = true); then you see that this whole statement has a value of true meaning you effectively created an infinite loop that will freeze your browser because it is not able to finish this task no matter how hard it tries and how much RAM it consumes. The comparison operators are == (compares values) and === (compares values and types).

The general idea of a while loop is that the code is run while the condition is true so it goes:

check condition --> true
-> run code
check condition --> true
-> run code
check condition --> false
go on after }

So in your case we start with understand being true so the condition is literally true, than we run the code, console.log the message and set understand to false. Now the condition is checked again but this time understand is false and so you go on.

One last word to understand === true vs understand:
As long as understand is a boolean there is no difference as you can easily prove by looking at the possible results:

understand === true is true if understand is true
understand === true is false if understand is false

so the outcome of understand === true is always what understand initially was.


Hmmm thanks for the input! It really helped.

But I think one of my main concerns is what While(variable) actually means as a condition. To me, it doesn't seem like a condition at all. What is it asking the computer to check?

Also, in response to your last bit:

understand === true is true if understand is true
understand === false is false if understand is false

How can the second line be true?
understand === false is false, IF understand is false?

If it IS false, shouldn't understand === false be true?


Sry my fault of cause it needs to be understand === true (changed it) as I want to look at the two possibilities for this.

Well where I used condition you should use anything that has a boolean value, that could be a real boolean or something that evaluates to a boolean like A === B or A < B aso. A real boolean is probably a bad idea as true never stops and false never begins but using a variable is fine as you can change the value in the progress and thereby let the loop do it's job.