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

Spencer Renfro
PLUS
Spencer Renfro
Courses Plus Student 11,133 Points

What does assigning a boolean value actually tell the computer?

After assigning a boolean value of true and false with variables, prompt diaglog for user input and everything, what do the values of true and false truly tell the computer? What I mean is if you can set the value of the variable to be false and write to the console to say it is in fact true but given the false boolean value in the code, then is assigning these values of true and false only intended on making it easier for the human to read?

1 Answer

Steven Parker
Steven Parker
231,172 Points

A boolean is like any variable in that it stores data. The restriction is that the data can only have two possible values, true or false.

But this statement is quite remarkable:
  "...you can set the value of the variable to be false and write to the console to say it is in fact true..."

That doesn't seem possible, can you provide a code example that does this?

Spencer Renfro
Spencer Renfro
Courses Plus Student 11,133 Points

So I wrote code to be the exact opposite of Guil's code from this course. I am confused as to what the true and false values mean if it is not just to make it easier for the human to read. This is what I meant by the statement above. I have set the guess to be false but the console is written out to respond that the input is still correct.

My code:

let guess = true; const answer = 'blue'; const inputguess = prompt ("What color is the sky?");

if ( inputguess === answer ) { guess = false; }

if ( guess === false ) { console.log("That's correct!"); } else { console.log("sorry, that is not correct."); }

Guil's code:

let correctGuess = false; const number = 6; const guess = prompt('Guess a number between 1 and 10.');

if ( +guess === number ) { correctGuess = true; }

if ( correctGuess === true ) { console.log('You guessed the number!'); } else { console.log(Sorry. The number was ${number}.); }

Steven Parker
Steven Parker
231,172 Points

I see what you mean now. As the programmer, you get to decide what each boolean state represents. In your example, false represents correct and true represents wrong. That's fine for the computer but it might e a bit confusing for someone reading the code.

But the name of the variable also makes a difference to the reader (but not the computer). So if you changed your variable name from "guess" to "wrong", then the way the boolean values are being used would make more sense (but still not as clear as the conventional way).

Certainly making the program operate is the most important job in programming, but making the code clear, efficient and concise have value as well. Doing all together is known as "best practice".