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

Understanding the "Or operator"

Hello!

I am very confused by the example given in this video for the "or operator".

Dave gives this example: say you want a user on your website to be able to type in either "yes" or "y" to agree to the website terms.

So you would write this:

(agree === 'yes' || agree ==='y')

The "Or operator" asks "is condition 1 true? Or is condition 2 true?"

agree === 'yes' : true agree === 'y' : false

In this case, 'yes' is true so that's enough. The entire statement is true.

But what if the user types 'y'. And we know that 'y' is FALSE, so how does that allow the statement to be true and thus allow the user to proceed on our website?

Thanks, Liz

What do you mean?

1 Answer

andren
andren
28,558 Points

If 'y' was entered then agree === 'y' would be true. The example in the video shows how the conditions would be evaluated in the scenario where the user answered 'yes'.

It was shown to illustrate that only one of the conditions provided need to be true in order for the if statement to run when you use the OR operator.