A fellow of infinite loops: How true is true(TRUTHINESS)


#1

I am having difficulty understanding this statement from the exercise

understand = true;  
while(understand)

Is it saying > while the variable understand is a true statement proceed with the code block?

and why doesn't this work?

understand = 4
while(understand = 4){
console.log("something");
understand = 5
}

Does it have something to do with how I declared the variable? What am I missing here?


#2

While declaring a variable, you must use the keyword var like this:


#3

But in the syntax of the original exercise does not declare a variable. it looks like this

 understand = true;

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

Also, when I declared the variable they way you suggested it still crashed my browser.


#4

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


#5

A while loop works like this:
check if the condition (the stuff in () after while) is true and if yes proceed with {} (block of code) and if not jump to the point after the }. So if the condition is always true it goes:

check condition -> run {} -> check condition -> run {} -> ...

in other words a loop. So in order to prevent infinite loops you need to make sure that there is a condition that can be turned to false. Counting towards a specific number, comparision with something random, user input or anything like this

Now the problem with your loop is this:

while(understand = 4){

Want you want to do here is to check if understand is 4 which it is at the beginning and which is then turned to 5. The concept is alright, the problem is that is not what this does. As you use = instead of == or === you don't compare but assign the value of 4 to understand regardless of what it has been before. As you can test an assignement always has the value that you assign so in this case it would be 4 (just run console.log(understand =4); to verify this). Now if you dig a bit deeper you find out that all numbers except 4 have a truthy value meaning if you use them in a condition they act like true:
http://www.sitepoint.com/javascript-truthy-falsy/
(this is not part of the track I just mention it to explain the error)


#6

I think I get it. Since the = assigns a value the statement understand = 4 is always true. No matter if it's equal to 4 or five. So does the code between the () have to be a Boolean? Also, You loose me here.

As you can test an assignement always has the value that you assign so in this case it would be 4 (just run console.log(understand =4); to verify this). Now if you dig a bit deeper you find out that all numbers except 4 have a truthy value meaning if you use them in a condition they act like true:


#7

I'll give it another try.

if you use understand = 4 you assign the value 4 to understand so now understand is 4 no matter what it has been before.
But the whole statement itself (understand = 4) also has a value:

console.log(understand = 4); --> 4

which happens to be the value you assign, which is in this case 4.
So effectivily the value in your while loop is:

while(4){...}

with the sideeffect that you changed the value of understand to 4.

So as the while loop always expects a boolean in its () it will turn anything used there into a boolean even if it is not a boolean. So I'd strongly recommend that you only use booleans or conditions that evaluate to a boolean (a ===b, a<b, a>= b, aso) or otherwise have a close look at the link to see which value is translated to which boolean. For the value 4 the links states that every number except 0 is treated as true so 4 is equal to true (at least in this case). Which than creates the infinite loop.

Is this better?


#8

Yes. Thank you very much!! :smile: