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 trialSamantha Atkinson
Front End Web Development Techdegree Graduate 40,307 PointsConditional Statements and Boolean values Have I Understood Them?
Hi someone has probably asked this question, however, I want to know if I have understood everything, so far:
-
A conditional statement is the if ( ) { } you don't have to add the else part, the if () {} is considered the conditional statement?
- Conditional statements, the if part if ( ) { } only return true or false?
- Booleans are values themselves true or false values?
- So by adding the boolean value false in the if ( false ) { } the if condtional statement is the reason why the else condition kicks in Dave's demo of the boolean values.
I think why it's confusing is because before we were adding variables with values in them and using the strict Equal To operator to test if the condition was true or false. However, by using the boolean values we are actually giving the conditional statement the outcome or value of the if statement basically without having to test if true or false?
I hope how I have worded it makes enough sense that someone can tell me if I have truly understood it all.
Thanks in advance for any answers
2 Answers
Steven Parker
231,275 PointsIt sounds like you have most of it. To confirm:
- an "if" is a conditional statement (with or without an "else" following it)
- the expression inside an "if" evaluates to true or false
if ( <expression goes here> ) ...
- Boolean values are either true or false
- the body of an "if" that has "false" as its expression will never run
- the body of an "else" that follows an "if" with "false" as its expression will always run
And don't worry, all of these concepts will become more clear as you gain experience working with them.
Jamie Reardon
Treehouse Project ReviewerHey Sam!
To answer your first question, yes! A conditional statement can be declared with an if keyword and the parenthesis. You don't have to add an else clause if your program doesn't need it. It is just a fall back for when the condition check fails and you want to run some code.
You can also add an else if clause for multiple program outcomes. The first else if clause will only run if the first if condition fails, and so on. Therefore, keep in mind that only one condition code-block will ever run.
Conditional statements, the if part if ( ) { } only return true or false?
The if statement by default, checks for a true condition. So, if you have boolean values, you could pass the variable holding the boolean value into the condition. For example:
let correctGuess = true;
if (correctGuess) {
// code
}
You can switch the default behaviour by using the not operator like so:
if (!correctGuess) {
// code
}
Booleans are values themselves true or false values?
Not sure I understand this question? But from what I can understand, you are asking if booleans are values like other data types such as numbers and strings? if so, then yes and there are only two: true and false.
Samantha Atkinson
Front End Web Development Techdegree Graduate 40,307 PointsHey Jamie, thank you for replying TechDegree buddy 👊. Oh wow, you gave me a whole lot more too, then just the answers to my question. I was just going over the video and kind of stopped to process at the point Dave just creates a conditional statement demo just using the booleans true and false as the expression at the beginning of the video.
I needed to know if I was getting the concept before he then starts with giving the variable correctGuess the value false because at that point I thought I need to watch from the beginning again lol.
Jamie Reardon
Treehouse Project ReviewerHey Sam!
Haha no worries! :) Glad it gave you some insight into conditional logic in js! I also struggled to understand the concept at first as well! But just to keep on mind, if you ever see or create a variable that holds a boolean value, it can be used as the condition since the conditions are only really searching for true or false checks!
See you on slack! :)
Samantha Atkinson
Front End Web Development Techdegree Graduate 40,307 PointsAwesome Jamie, thank you!!!
Samantha Atkinson
Front End Web Development Techdegree Graduate 40,307 PointsSamantha Atkinson
Front End Web Development Techdegree Graduate 40,307 PointsThank you, Steven, once again coming to my aid. I hope they do become clearer with experience. Thanks for answering with technical terms, like expressions.