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 Combining Multiple Tests Into a Single Condition

Can you mix && (and) and || (or) operators in the same conditional block?

for example:

if ( true && false || true ){....

if so, how would it run. What boolean would it create ?

if not, would it be possible using parenthesis inside the conditional statement?

nico dev
nico dev
20,364 Points

Hey found your question interesting, especially when you mentioned the parenthesis, really caught my curiosity.

I found some interesting info here that includes a lot of different scenarios and in-depth explanations.

Hope that helps you also, or someone else whose curiosity demands to be answered (like mine!) :)

1 Answer

Per Karlsson
Per Karlsson
12,683 Points

Yes you can mix them. Try this out:

var one = true;
var two = false;
var three = true;

if (one && two || three) {
  console.log("YAAAAY");
}

Try changing the variables around between true and false.

true && true || false = true
true && false || true = true;
false && false || true = true;
true && false || false = false;

I'd imagine it'd become incredibly complex if anyone did it to any more depth, but it's great that the language is versatile enough that it can be done.

Thanks!