What's this for?


#1

I don't understand this line: understand = false; in this code:

var understand = true;

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

#2

I think it's mean't to be like this. You haven't put understand in the while part.

var understand = true;

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

The understand = false stops the loop because otherwise understand will always be true which means it will keep printing I'm learning while loops. so you then make the understand variable = false which stops the loop from running. It's just stopping an infinite loop forming.

If that doesn't work change the while loop to be

while(understand === true)

and keep the rest the same.


#3

now I have another question.. why doesn't work also with number like so:
var understand = true;

while(understand === 1){
console.log("I'm learning while loops!");
understand = false;
}
I have an intuition ..I'm thinking about the block code in while loop is not the same format:
I mean 1 - is a number variable
true - is a boolean variable
Is that correct?


#4

Yes true is a boolean, and 1 is a number variable.

I believe it wont work with a number because I think you've declared understand as a global variable which is equal to true.

You could get it to work with a number if you did this.

var understand = 0;

while(understand < 5) {
console.log("Im looping");
understand++;
}

This would print im looping 5 times, because understand is less than 5. understand++ means keep going through the loop adding one until understand = 5. This will then cut the loop off.