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 trialGrace Ji
5,402 PointsI watched this video 5 times, and still can't understand at all.
I feel frustrating. :(
3 Answers
Steven Parker
231,236 PointsDon't over-think things.
Booleans are the most simple kinds of values there are. They can only be "true" or "false". Or think of them like light switches — they can only be "on" or "off".
If that doesn't help clear things up, perhaps you could explain a bit more about what you find confusing.
Mezzo Forte
1,877 PointsI'd assume it has to do with how come the if statement isn't comparing the Boolean variable to a true or false keyword.
var correctGuess = true;
if ( correctGuess ){
alert("You guessed correctly!")
}
else {
alert("You guessed incorrectly")
}
what is the correctGuess variable being compared with?
if ( correctGuess ){ }
is really
if ( correctGuess === true){
}
Its better to write code out completely sometimes since it increases readability. I myself forgot that its possible to just write if(true){}. Rarely do I write it that way in C# or other languages.
Steven Parker
231,236 PointsFollowing best practices, you would not actually write (correctGuess === true)
, since the boolean correctGuess represents true or false on it's own.
But for extra clarity, a good practice would be to add a comment to the line explaining the intention of the logic.
Mezzo Forte
1,877 Points@Steven, you are correct. Did some googling to look more into it and found this. Much appreciated, your comment has been helpful. Learned lot from it! :)
http://stackoverflow.com/questions/23061921/javascript-ifx-vs-ifx-true/23061950
Though in the stackoverflow link they did use == instead. Will test when I have time.
John Silverstein
6,290 PointsI've yet to find a clear explanation of the logic behind not stating correctGuess === true. is it the case that a boolean value needs only be defined once, and from there on everytime its mentioned it switches between true and false?
Steven Parker
231,236 PointsA boolean (or any variable for that matter) doesn't change just from mentioning it.
You would need to assign it a different value to change it. For example, if you have this code:
yes = true;
if (yes) alert("yes is true");
if (yes) alert("yes is STILL true");
Both messages will pop up since "yes" is never changed.
And comparison operators (like "===") compare two values and return a boolean result. That's why you don't need comparisons if you have a boolean to start with, you just name it.
Savannah Lynn
13,662 PointsSavannah Lynn
13,662 PointsWhere in the video are you getting stuck? Boolean values as examples can sometimes be so simple they are confusing. Did you understand the previous video?