Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

while(! correctGuess) ?

I get the whole process of making a true or false flag and making the variable.

I don't get why the ! correctGuess and all the rules implicated to it.. can someone explain?

michaelcodes
michaelcodes
5,604 Points

I don't have much JS experience but in most languages ! means not. So this would be saying while NOT correct guess

3 Answers

Steven Parker
Steven Parker
231,275 Points

As michael guessed, the "!" symbol is the logical "NOT" operator. So "while (!correctGuess)" means "keep looping if we still do not have a correct guess". When the value of "correctGuess" is set to "true" inside the loop, the loop stops repeating.

Garrosh HellScream
Garrosh HellScream
7,503 Points

while (!correctGuess) is confusing. Whats wrong with using while (correctGuess == false)? It means the exact same thing right? Matter of preference?

Steven Parker
Steven Parker
231,275 Points

Sure, if you find that that easier to read, it is a functional equivalent. But it is not as efficient to execute.

Want to thank everyone for the help and the correct guidance!.

Yep, michaelcodes is correct. the !, is called the Logical Not Operator (sometimes called a "bang"). In your code, the while loop will run until its condition returns false, i.e. until "NOT correctGuess" is true, or the guess is incorrect. Here's the Mozilla Dev Network page on Logical Operators: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators.