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

Joseph Hookham
Joseph Hookham
2,493 Points

How can simplifying the code to "if (correctGuess)" be both true and false?

At the end of the video on Boolean values he says we don't need to use:

if (correcGuess === true)

We can just use:

if (correctGuess)

because we have true / false embedded already. But how does it know which one we want to refer to?

Does the word 'if' assume 'true' whilst 'else' assumes 'false'?

Thanks in advance.

2 Answers

Adam Pengh
Adam Pengh
29,881 Points

An if statement is a conditional statement that takes an expression that is considered to be either truthy or falsy. Since the variable correctGuess is a boolean that will return either true or false, there is no need to add the === true. You can also test for falsy value by using the ! operator like so:

if (!correctGuess) {
  // Code to execute if correctGuess is false
}
Joseph Hookham
Joseph Hookham
2,493 Points

Sorry that still hasn't cleared it up.

When the program looks at the code:

" if (correctGuess) "

what indicates the choice between those 2 options? (true / false)