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 Boolean Values

Derek Sellar
seal-mask
.a{fill-rule:evenodd;}techdegree
Derek Sellar
Full Stack JavaScript Techdegree Student 1,339 Points

correctGuess

Can someone explain why in the second conditional statement we could just write If ( correctGuess) instead of defining it as true/false?

I'm not sure I'm understanding how the code would automatically assume that correctGuess would be true rather than false.

2 Answers

When doing β€œif checks”, assignment operators can sometimes be omitted.

Longhand:

if (likeJavaScript === true)

Shorthand:

if (likeJavaScript)

Note: these two examples are not exactly equal, as the shorthand check will pass as long as likeJavaScript is a truthy value.

Here is another example. If β€œa” is NOT equal to true, then do something.

Longhand:

let a; if ( a !== true ) { // do something... }

Shorthand:

let a; if ( !a ) { // do something... }

https://www.sitepoint.com/shorthand-javascript-techniques/

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

if correctGuess contains ANY value (except for 0 or false) it is considered true

const correctGuess = 'Bob';

if (correctGuess) {} // evaluated to true

const correctGuess = '';

if (correctGuess) {} // evaluated to false because '' counts as "no value"

const correctGuess = true;

if (correctGuess) {} // evaluated to true