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 Making Decisions in Your Code with Conditional Statements Boolean Values

Adekite Parker
seal-mask
.a{fill-rule:evenodd;}techdegree
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

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.