Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics Making Decisions in Your Code with Conditional Statements Boolean Values

Adekite Parker
Adekite Parker
Full Stack JavaScript Techdegree Student 1,702 Points

let correctNum = false;

Why do i need the variable "let correctNum = false" when I could just use a two part if statement that basically says: if(+guess === number ){execute code} else{execute code}.

arent all if statement boolean, anyway?

1 Answer

Jason Larson
Jason Larson
7,739 Points

Yes, all if statements are boolean. In this very simple example, you don't need to have a separate variable. If you're just returning a statement based on the result of the condition, you don't need to have a separate variable. Boolean variables are used to save time when comparing against parts of objects so you don't have to type in the whole thing every time, or to limit scope of variables, or when you want to check a condition, set a flag, then do something and change the flag back. They are also very useful for indicating if a condition has been triggered by various different functions. For example, you might have a boolean variable called Success, and you check a bunch of different things, and if any of them give a bad result, you set Success to false, then at the end, you do if (Success) {}. There are lots of reasons for using them, but in this video, Guil is just demonstrating how you would use it, but it's probably overkill for this small program.