JavaScript / Making decisions

else if chains

Choose between more than two cases with an if / else if / else chain, and order the conditions so the right one runs.

if-else chooses between just two paths. Many decisions have more options: first prize, second, third, or a small gift. Here you use else if to add a new condition that is tested only if the ones before it were false.

JavaScript
let num = 2;
if (num == 1) {
    console.log("New bicycle");
} else if (num == 2) {
    console.log("Pair of tickets to the aquarium");
} else if (num == 3) {
    console.log("10 bags of potato chips");
} else {
    console.log("Stickers");
}
Console
Pair of tickets to the aquarium

The program tests the conditions from top to bottom. At the first true condition it runs those commands, then skips the rest of the chain. If no condition is true, the final else runs; it is optional.

JavaScript
let people = 3;
if (people == 2) {
    console.log("10% off");
} else if (people >= 3) {
    console.log("20% off");
} else {
    console.log("retail price");
}
Console
20% off
Because only the first true condition runs, the order matters. Here the value 0 makes all three conditions true, but only the first line is printed, and the other two can never be reached:
JavaScript
let egg = 0;
if (egg <= 10) {
    console.log("Less than or equal to 10");
} else if (egg <= 5) {
    console.log("Less than or equal to 5");
} else if (egg <= 3) {
    console.log("Less than or equal to 3");
}
Console
Less than or equal to 10

The rule: start with the narrowest condition. For grades, start with the highest boundary, so each else if already knows the mark is below the boundaries before it:

JavaScript
let score = 77;
if (score >= 90) {
    console.log("Excellent");
} else if (score >= 75) {
    console.log("Very good");
} else if (score >= 50) {
    console.log("Pass");
} else {
    console.log("Fail");
}
Console
Very good

You can use && and || inside any condition in the chain:

JavaScript
let numberOfPeople = 1;
let fee = 3500;
if (numberOfPeople >= 2 && fee >= 3000) {
    console.log("Free mug");
} else if (numberOfPeople >= 2 || fee >= 3000) {
    console.log("Complimentary cookie");
}
Console
Complimentary cookie
else if is two words with a space (elseif is wrong), and every else if has a condition in brackets. The final else has no condition, and no else if can come after it.

Practice

1Edit the programCurriculum practice

Add a third condition to the chain: otherwise, if num equals 3, print 10 bags of potato chips.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let num = 3;
if (num == 1) {
    console.log("New bicycle");
} else if (num == 2) {
    console.log("Pair of tickets to the aquarium");
}
Console
Press Run to see the result.
2Fill in the blankCurriculum practice

Fill in the blank: if egg is not 0 but is less than or equal to 3, print I'm almost out of eggs.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let egg = 2;
if (egg == 0) {
    console.log("I have to go buy more eggs");
} ___ {
    console.log("I'm almost out of eggs");
} else {
    console.log("I still have eggs");
}
Console
Press Run to see the result.
3Fill in the blankCurriculum practice

Fill in the blanks: if num is less than or equal to 3, print A; else if it is greater than or equal to 6, print B; otherwise print C.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let num = 4;
if (___) {
    console.log("A");
} else if (___) {
    console.log("B");
} ___ {
    console.log("C");
}
Console
Press Run to see the result.
4Write the programCurriculum practice

Declare the variable test and assign it 77. If it equals 100, print Perfect score; else if it is greater than or equal to 75, print Pass; otherwise print Make-up exam.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
5Edit the programCurriculum practice

This program prints nothing. Add an else if: if the number of people is 2 or more or the fee is 3000 or more, print Complimentary cookie.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let numberOfPeople = 1;
let fee = 3500;
if (numberOfPeople >= 2 && fee >= 3000) {
    console.log("Free mug");
}
Console
Press Run to see the result.
6Write the programExtra practice

Declare the variable temperature and assign it 38. If it is 35 or more, print Very hot, drink water; else if it is 25 or more, print Warm; else if it is 15 or more, print Mild; otherwise print Cold.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
7Write the programExtra practice

Museum tickets depend on age: under 4 is Free, 4 to 12 is Child ticket: 20 pounds, 60 or over is Senior ticket: 25 pounds, and anyone else gets Adult ticket: 40 pounds. Declare age = 10 and print the right ticket.

HintBy the time you reach else if (age <= 12), you already know the age is not under 4.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • else if (condition) tests a new condition only if the ones before it were false.
  • Only the first true condition runs and the rest are skipped, so start with the narrowest one.
  • The final else is optional, has no condition, and runs when nothing else was true.