Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

I'm confused about the condition

I dont understand the condition he is using very well. His condition is (! correctGuess) i tryed it with (correctGuess === false) and it works the same as the initial variable correctGuess = false. Given my condition i understand exactly what's going on in the code. But how is (! correctGuess) the same with (correctGuess === false)? can someone help me

5 Answers

Emmanuel C
Emmanuel C
10,635 Points

The exclamation mark or "!" is the logical NOT operator, You can read that statement as If Not correctGuess. Basically it, If correctGuess is NOT true. If the variable correctGuess is false, then that mean its Not true, which makes the whole if statement true.

if(correctGuess === false) // check if correctGuess is equal to false

if(!correctGuess) //check if correctGuess is NOT true

if(correctGuess !== true) // check if correctGuess is Not equal to true

They are doing the same thing. I hope this makes sense.

Joseph Anson
Joseph Anson
14,447 Points

I get that it flips it to the opposite but from what I understand, if it's already set to false then the opposite will be true? So

var correctGuess = false;
console.log( ! correctGuess );  = true?

correctGuess would become true? But we need it to stay false until the if statement changes it to true. I really don't understand. Help!

Emmanuel C
Emmanuel C
10,635 Points

The variable correctGuess itself is false, but when you have the NOT operator your essentially asking if the correctGuess is NOT true, and because correctGuess is in fact false, so its not true, then its makes the entire statement true. But it does not reassign the variable.

Try to see conditions as asking a question. If you see


Then youre basically asking, is the correctGuess variable equal to true?

But if you see

if(!correctGuess) {}

Then youre asking, is the correctGuess variable NOT equal to true?

I could be wrong but you're basically commanding the while loop to keep on looping if

 correctGuess === false

It's just like saying

if(correctGuess === false) {
  return true;
} else {
  return false;

So you get true in return because correctGuess does equal false!

I get how that can be confusing. Even I was confused at first but it took some brainstorming. :)

Carl Evison
Carl Evison
2,656 Points

This confused me too, as it flips the boolean value to it's opposite. Doing this helped me understand it.

console.log( ! true); // returns false

console.log( ! false); // returns true

Hopefully that'll help someone else out.

Kishan P
Kishan P
9,921 Points

Think of like this way. Basically "!" is the logical NOT operator.