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 React by Example Building the Application Filtering and Changing State Review

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,678 Points

I don't understand why how logical expression evalutates

Question says:

"If the variable isFiltered is false, what would the following expression evaluate to? !isFiltered && 'showAll'"

I would have thought it was 'true', but it evaluates to 'showAll'. I don't understand why.

Abraham Juliot
Abraham Juliot
47,339 Points

There's a very informative video on short circuit evaluations at https://teamtreehouse.com/library/short-circuit-evaluation

I learned a lot from it. I do this so that others can read my code:

/*if*/ !isFiltered && /*then*/ 'showAll'

1 Answer

Nicholas Grenwalt
Nicholas Grenwalt
46,626 Points

Logical AND will only return true for the second expression if it literally evaluates to such as in doing a comparison (true && 12 > 5 for example), otherwise it will return whatever the actual value is. The string 'showAll' is true under the hood yes, but logical AND returns the value of that truthy expression, not the boolean representation of that value. The same goes for True && 'cat' would return the string cat. Hope that helps.