sourlemonaid sourlemonaid - 2 months ago 7
Javascript Question

Usage of !false 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 becomes true instead of false. So "while not false" the do-while keeps running? Or does this mean "while true" it keeps running or...?

thanks for the help!

Answer

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