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 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,997 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
14,244 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.