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.
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");
}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.
let people = 3;
if (people == 2) {
console.log("10% off");
} else if (people >= 3) {
console.log("20% off");
} else {
console.log("retail price");
}20% off
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");
}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:
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");
}Very good
You can use && and || inside any condition in the chain:
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");
}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
Add a third condition to the chain: otherwise, if num equals 3, print 10 bags of potato chips.
let num = 3;
if (num == 1) {
console.log("New bicycle");
} else if (num == 2) {
console.log("Pair of tickets to the aquarium");
}Press Run to see the result. Fill in the blank: if egg is not 0 but is less than or equal to 3, print I'm almost out of eggs.
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");
}Press Run to see the result. 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.
let num = 4;
if (___) {
console.log("A");
} else if (___) {
console.log("B");
} ___ {
console.log("C");
}Press Run to see the result. 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.
Press Run to see the result. 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.
let numberOfPeople = 1;
let fee = 3500;
if (numberOfPeople >= 2 && fee >= 3000) {
console.log("Free mug");
}Press Run to see the result. 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.
Press Run to see the result. 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.
Hint
By the time you reachelse if (age <= 12), you already know the age is not under 4.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
elseis optional, has no condition, and runs when nothing else was true.