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 JavaScript Basics (Retired) Making Decisions with Conditional Statements Boolean Values

i did not understand why you set the boolean to fales at the top? i still can not quite understand how boolean works.

i did not understand why you set the boolean to fales at the top? i still can not quite understand how boolean works.

3 Answers

Kevin Korte
Kevin Korte
28,149 Points

Booleans really are just true or false, there is no other value it can be.

Often times you'll see this pattern where a variable will be instantiated with a boolean, typically false, since often we check if something is true. Because the first thing we do is create a variable set to false, we know later in our program, when we check if the variable is true or false, we don't have to worry about it erroring because it's not set. It's a least set.

Than we can write code that checks other stuff, and change the boolean from false to true if we need to. Than we check the variable later on again, we know the first check happened if the variable now evaluates to true.

An example using real life

var wearingShoes = false;
var shoesTied = false;

//I can't tie my shoes if they aren't on my feet, so first I need to see if I'm even wearing shoes

if ( wearingShoes ) {
  shoesTied = true;
} else {
  wearingShoes = true;
  shoesTied = true;
}

So by checking boolean values, I can make sure that something happens, or is true before another thing happens. In this case, I check to make sure I'm wearing shoes (if true) before I try to tie them. If I am wearing shoes already, I can simply just tie them. If I'm not wearing shoes, I first need to put shoes on, and than tie them.

All booleans do is help us keep track if something is true, or is false. What is true, and what is false depend greatly on what you as the developer, need to check.

Finally got the concept after reading your explanation and I was reading a lot. So thank you.

a really helpfull comment! thx

David Chapman
David Chapman
22,023 Points

Hi Fadi Walter,

I'm assuming you're referring to the exercise that Dave sets up for you in the workspace attached to this video.

  1. The variable correctGuess is set to false just as a default. Generally, we set up (or define, etc,) variables that we will be using later so that we're not doing so on the fly, mainly so they're everywhere (global scope).
  2. The essence of Boolean is very simple (something is either true or false), but it's effects can be unexpected at first (so don't feel bad if it doesn't totally make sense yet). As an example, it may have not made sense when Dave showed you something like this:
if ( false ) {
  "x is true" ;
} else {
  "x is false";
}

and see the result print "x is false". Just remember that true and false don't need anything else to explain them—they are a "returned value" in themselves. And since if/else statements are looking for true/false, to determine which bit of code is executed, then all you need is a true or false word in the conditional statement (the parentheses after if).

Hopefully this helps!

ryu shorokuken
ryu shorokuken
414 Points

Thanks for the explanation! You explained it so much better than Dave did.

Steven Parker
Steven Parker
231,271 Points

Boolean variables are really very simple.

Boolean variables can only hold two values &mdash either true or false. So they are much simpler than numeric variables which can hold many different values.

There's nothing special about setting a boolean variable to false at the program start. It's very similar to setting a numeric variable to 0. In both cases, you are just establishing the starting value.