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

Joseph Overton
Joseph Overton
4,234 Points

= within a conditional statement.

I made a coding error which brought up an interesting question. In the second if statement I accidentally put if ( correctGuess = true ) instead of if ( correctGuess === true ). The result was my program always returned that the input was correct, despite the number entered.

SO my initial assumption from this was that the act of assigning a value to a variable can't be a condition in this type of statement. But then why would this return the result of the if statement, rather than the else, or just an error?

The assignment must be happening regardless of it being contained in statement.

Is it both assigning a value and making that assignment a condition? When I leave the brackets empty I get an error returned in the console, so something is happening.

This is my first dive into coding, so I realize this will probably be covered later in the course, but I figured I'd ask!

Incorrect code posted below... Thanks! Joe

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}.); }

Josh Icenogle
Josh Icenogle
9,257 Points

So anything inside of the if statement will run if the condition returns a boolean of true and whenever you assign a variable a value(which is what you did with "correctGuess = true") it will automatically return true as long as it's value coerces to true or false if it's value coerces to false . Try console.log(Boolean(x = true)) and console.log(Boolean(y=false)) and you'll see what I mean. Here's a link that might be helpful https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/

2 Answers

Fran ADP
Fran ADP
6,304 Points

Yes, you are right.

Steven Parker
Steven Parker
231,268 Points

You guessed right, the assignment happens first and then the result is tested as a condition. An assignment returns the value that was assigned, so setting something to "true" will always be true.

I've seen this used (and admittedly done it myself) to keep code compact, but it can easily be misinterpreted during hasty code reading so I wouldn't recommend it.