sourlemonaid sourlemonaid - 1 month ago 4x
Javascript Question

can someone help explain !false to me in a do while loop

i saw a do while loop that looked something like

var guess = false;
do {
stuff stuff stufff
if ( things === things){
guess = true;
} while ( ! guess )

this confused me because the ! operator changes the boolean value to the opposite? so that guess because true. instead of false. so while not false the do while keeps running? or does this mean while true it keeps running or...? i can't seem to get it.

bascially what i am saying is i'm not sure why this loop keeps looping. what the "! guess" means here. i am having trouble understanding the "!guess" part since guess is set to false. does while(!guess) make guess true? if so it's true now and shouldn't the program stop? or does it keep running while guess is not true? and while(!guess) doesn't change guess to true just checks to see if it is not yet true?

i am trying to make sure i understand this properly. sorry for the crappy wall of text

any explanation would be awesome


while(!guess) means "while guess is not true".

It also can be written as while(guess == false). Maybe that way is easier to understand, although it is not a good practice.

There are some examples here: MDN - Logical operators