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

Kristian Woods
23,414 PointsHow do boolean values work with conditional statements?
Hey, just want to double check my foundational knowledge with javascript, especially in regards to boolean values and conditional statements
if a boolean value is initially set as "false", then later down the line, changes to "true" - if the boolean value is used in an "if" statement, and holds a value of "true", how does the "if" statement know what that means? Simply put, how does the "if" statement know we're asking if the value held in "accepted" is true? Because, surely, you could also see it as "if boolean value equals false then run script"?
var accepted = false;
if (userName.value === users && password.value === logIn) {
accepted = true;
}
if (accepted) {
// do something
} else {
do something else
}
Thanks
2 Answers

Jesus Mendoza
23,289 PointsHey Kristian
JavaScript code executes from top to bottom, so when it gets to the if statement it checks for the value stored in the variable "accepted" and since it already changed to true then executes the code inside of it.
The code inside if, else if, switch, loops, etc are only executed if one or more conditions are true.
if (2 === 2) // is equals to true so the code inside if statement always runs
if (true) // will always run
if (2===3) // equals to false so it wont run
if (!false) // is true so it will run
if (true || false) // will run because one condition is true
if (false && false) // won run because both conditions are false
if (false === false) // will run because it's true. false its equal to false.

Kristian Woods
23,414 PointsDude, this is an awesome explanation! thanks
Vance Rivera
18,322 PointsVance Rivera
18,322 PointsThe if statement checks if the condition given is true otherwise it is false.
Hope this helps. Cheers!