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 Basics (Retired) Making Decisions with Conditional Statements Boolean Values

Why do we get rid of the equality operator?

I understand (I think) why we set the the first variable to the boolean value false at the beginning but I don't understand how we are able to totally remove the equality operator all together in the second 'if else' statement.

if ( correctGuess ) {
  document.write("<p>You guessed the number!</p>");
} else {
  document.write("<p>The correct number was" + randomNumber + "</p>");
}

I know the variable correctGuess becomes either true or false from the previous block but I dont understand how just having the variables true or false value will tell the code what to execute.

Ugh Im just not grasping it yet, any help would be greatly appreciated!

2 Answers

Christopher Lebbano
Christopher Lebbano
15,338 Points

So in a conditional statement such as this one, you are checking to see if "correctGuess" has a true or false value.

The conditional statement: if ( correctGuess )

is the same as: if ( correctGuess === True )

The else statement is the code block that will run if "correctGuess" is not true, or false.

This is different than an "else if" statement. In an else if statement, you have to code in another conditional branch.

Example:

if ( x === 3 ) { Code Block that runs if x is 3 } else if ( x === 2 ) {Code block that runs if x is 2) else {Code block that runs if x is neither 3 or 2}

Thank you!

Matt Milburn
Matt Milburn
20,786 Points

Hi Nikki,

We can omit the comparison operator because the condition will always evaluate to either false or true unless there is a syntax error. Check out MDN's documentation on the terms Truthy and Falsy for more info on the subject.

Thank you!