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

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?

1 Answer

Steven Parker
Steven Parker
210,474 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