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 Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

azraharbas
azraharbas
1,095 Points

Best practice for boolean conditions?

Hi,

I feel that I rather use easily understandable condition as (correctGuess === false) instead of (! correctGuess). Is there a "best practice" rule for which one to use?

1 Answer

Cooper Runstein
Cooper Runstein
11,850 Points

Both are equally understandable for what they are, but are not the same thing. For example:

var myVariable;
myVariable === false; //this is false
!myVariable; //this is true

The first checks if the variable is equal to the value false, while the second checks to see if the variable is not truthy. In the context of whatever problem you're solving now that may not matter, but in many situations there is a need for one or the other. I'm assuming you're somewhat new to programming, the more you code and read others code the more you'll get comfortable with some of the syntax such as '!'. You'll even sometimes see people write something like:

!!(someVariable)

in order to make it clear that they only care about the truthynesss of someVariable, and the actually value is irrelevant to their code. Hope this helps! Let me know if I can make anything clearer for you!