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 JavaScript Basics Making Decisions in Your Code with Conditional Statements Introducing Conditional Statements

Introducing Conditional Statements

What if I had more than one correct answer i wanted to put with the "if" statment? How would I write this then?

2 Answers

Jamie Moore
Jamie Moore
3,951 Points

Hey Paul,

That would depend on whether you wanted one of your conditions to be true, or all of your conditions.

In Javascript you have access to the or operator, written as ||, and the and operator, written as &&.

// This is an example of using the 'and' operator
if (a > b && a > c) {
    console.log(`a is greater than both`);
}

// This is an example of using the 'or' operator
if (a > b || a > c) {
    console.log(`a is greater than a, b, or both`);
}

In the above example, the and operator checks if both conditions are true, and only proceeds if both are true. The or operator checks if either of the conditions are true, so the code will run if either of the conditions are true.

Hope this helps!

Gergely Bocz
Gergely Bocz
Treehouse Moderator 14,215 Points

Hi Paul HILL!

Jamie Moore has got your back! I would just like to correct a small typo on his part.

In the second example, instead of:

if (a > b || a > c) {
    console.log(`a is greater than a, b, or both`);
}

He meant to write this:

if (a > b || a > c) {
    console.log(`a is greater than b, c, or both`);
}

Thanks guys. I kind of pieced it together later in the videos. But this glued it together for me.