Else If Statement

Sometimes we need the program to handle more than 2 options (if, else). Therefore, we can add more conditions using the else if statement. For example, in football, the home team can win, the away team can win, or it can be a draw:

const homeGoals = 3;
const awayGoals = 2;
if (homeGoals < awayGoals) {
	console.log("The away team won!");
}
else if (homeGoals > awayGoals) {
	console.log("The home team won!");
}
else {
	console.log("It's a draw!");
}
// Prints: The home team won!

Here we can see 1 else if statement, but we can add as many as we want.

It's important to understand that conditions are evaluated from top to bottom. This means that the block of the first condition that evaluates to true will be executed. In the example above, homeGoals < awayGoals is false and homeGoals > awayGoals is true, so the code block of the first else if statement is executed and the rest of the conditions are skipped. If no condition is true, the else block is executed.

Instructions

Use an else if statement to print Just Do It if the brand is "Nike".

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

5/9

Else If Statement | Start Coder