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

The boolean

Hello can somebody explain to me, what is all about, about boolean, so it's false and true, so what? what can i do with that, please give me detailed information with examples!

3 Answers

A boolean variable type is valuable when you have a situation that you want to be true OR false. Think of it as an on/off switch.

var isLightSwitchOn = false;

function flipLightSwitchOn() {
    isLightSwitchOn = true;
}

While you could technically do the same thing with other types, such as setting a variable to either 1 or 0 for off/on, it's a much simpler and cleaner way to do it. You'll find that when your program complexity grows, constraints help with readability and understanding of your code and processes.

Hi, Matthew, can you give me example for situation, to be true or false?

Here's a few examples of booleans:

If you had an application with checkbox a user can check to remember their login username, the checkbox would have a checkbox.checked would be a boolean that could equate to true OR false.

As given above, a light-switch could also emulate a boolean concept, if the light-switch is up, lightOn would be true, if it's down, it would be false.

when you evaluate if-expressions, it checks for a boolean if (x < 2) {code} , "x < 2" is actually checking a boolean value, true or false. The if-statement will only run the code if the value is true.

Matthew this part of boolean I understand is this if (x < 2) {code), but I dont understand this part, when the boolean is used inside of the code it's like this >> a = true; or return true; So what a is true and return, returns true