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 Improving the Random Number Guessing Game

Why does he start off with a "false" variable?

i dont understand why he starts off with a 'false'?

3 Answers

Steven Parker
Steven Parker
231,271 Points

An "undefined" is considered equivalent to "false" in an evaluation; so technically, it would make no difference to the program to omit the initial setting.

But starting with "false" makes it clear what the intentions are for that variable, plus it's a "best practice" to always set an explicit value rather than relying on implicit handling to take care of it.

Awesome, thank you for the correction @steven Parker :)

Wooohooooo awesome you got it! And if you had nothing attached to the variable it will automatically be given a value of undefined, which basically just means that there is no proper value yet associated with that variable.

Which means the player must guess 'undefined' to make it work... and thats really messy, thats why its best to give it a false value so the program can ACTUALLY do something with it...

The correctGuess variable is given a value of false so that different conditions can be tested. Think of it like a starting variable, it allows your random generator game to work. When the value is false, the player has to keep guessing until his guess matches the random number. Once there is a match, the correctGuess variable will have the value true.

Oh i get it! if it were true right of the back, then the program would have no reason to keep going?

what if we didn't add a "false" from the beginning? What would happen?