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

Dylan Carter
Dylan Carter
1,046 Points

Why is this method better than a just asking if their guess was 6 for the conditional statement?

it seems like it would be a lot easier to do

let guess = prompt("Guess a number 1 to 10");

if (guess === 6) {

console.log("thats correct")

}

else {

console.log("thats incorrect")

}

is there a reason storing the value as a boolean is better here? or is this maybe just a dumbed down example for understanding the concept and it will be applied better in the future?

2 Answers

Steven Parker
Steven Parker
231,172 Points

You're right that the example is to illustrate working with booleans and isn't necessarily the best way to handle a particular task.

However, you couldn't use the type-sensitive equality operator to match a string with a number. But you could do:

if (guess === "6") {  // match string with string
// ...or...
if (+guess === 6) {   // match number with number
// ...or...
if (guess == 6) {     // use normal (type coercing) comparison
James Summers
James Summers
704 Points

Because then someone could lie to the computer