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

How 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

Vance Rivera
Vance Rivera
18,322 Points

The if statement checks if the condition given is true otherwise it is false.

var accepted = false;

//This code will not run because the condition evaluates to false
if(accepted){
  console.log('Yay! I have been accepted. Accepted value is :' + accepted);
}

//This code will run because the condition evaluated is true at this point
if(!accepted){
  console.log('Oh No! I was rejected. Accepted value is :' + accepted);
}

//Now lets update accepted to true
accepted = true;

if(accepted){
  console.log('OMG! It was a mistake and I was accepted. Accepted value is: ' + accepted);
}

//output : Oh No! I was rejected. Accepted value is : false
//output : OMG! It was a mistake and I was accepted. Accepted value is: true

Hope this helps. Cheers!

2 Answers

Hey 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.

Dude, this is an awesome explanation! thanks