JavaScript Resources

else if Conditional Statement

Think of using else if when you want to extend the number of possible outcomes in a decision making structure beyond two outcomes, i.e. when you have 3 or more possible outcomes in mind.

The else if conditional statement is always used in collaboration with the if and else conditional statements. As a matter of fact, it must be placed between these statements.

The if/else if/else structure looks like this:

    if (some condition that evaluates to true) {

      then execute this statement

      and any other statements on additional lines

    }

    else if (some other condition that evaluates to true) {

      then execute this statement

      and any other statements on additional lines

    }

    else if (some other condition that evaluates to true) {

      then execute this statement

      and any other statements on additional lines

    }

    else {

      then execute this statement

      and any other statements on additional lines

    }

Similarly to the if/else structure, only one of these sets of statements will execute.

If none of the conditions are met, then the statements in the else structure are executed.

Conditions are evaluated in the order that they are used in the if/else if/else structure. So, if two or more conditions are actually true, only the first condition that is encountered will execute.

You are not limited to three or four paths. You can have as many as you want, just insert an additional else if structure for each additional path.

Next up ... Conditions