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 Programming Multiple Outcomes

gaby ibarlucea
gaby ibarlucea
4,213 Points

If vs else if

Is if only used once in the conditonal statement? as in

if ( ) { } else if ( ) { } else if ( ) { } else if ( ) { }

or can you have a conditional statement that looks like this? >>

if ( ) { } else if ( ) { } if ( ) { } if ( ) { }

3 Answers

Jason Wiram
Jason Wiram
42,762 Points

The "if" by itself is only used once per conditional (at the start). Additional "else if" clauses that follow will only be executed if your first "if" statement evaluates to false. So in your first statement, as soon as one of the if, or else if, statements evaluates to true (and the code block runs), you are done. Nothing else is executed.

In your second statement, there are three separate conditional blocks. The first "if" and "else if" will function the same as above. Like the first statement, the "else if" maybe or may not be evaluated. However, every single "if" statement after that will be evaluated no matter what happened before it. They are separate entities and not tied to the "else if" at all.

This resource may help as well: https://www.w3schools.com/js/js_if_else.asp

thanx, guys!!!

** Yes if statement is used once else if * is used as you wish but as long as you use else if you have to terminate it with *else **