It makes no sense how while loops operate?!


I made the code correctly, cus I found it in a thread. But can somebody explain what happens here?
Does the while loop check if understand equals true and if so it prints out false?
To me it makes no sense, hope someone can help

var understand = true;

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


var understand = true; //here you took a variable named understand and set it to true


while( understand) // here it means while( understand==true)

The while loop loops as long as a specified condition is true.

so here while( understand ) it checks understand equals to true or not.As it's true
now console.log("I'm learning while loops!");

then you set understand = false;   //it's very important

becuse now it'll again check understand equals to true or not.this time it's it'll end now.but if you forget to set it to false the loop will never end.This will crash your browser.

hope you've understand now :slight_smile:


Yes, makes a lot more sense!
It was the (understand) and (understand === true) meaning the same thing that hadnt been explained that confused me..


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.