34. Control Flow: else if Chains
When you have more than two possible outcomes, you can link multiple conditions together using else if.
Syntax
javascript if (condition1) { // Runs if condition1 is TRUE } else if (condition2) { // Runs if condition1 is FALSE AND condition2 is TRUE } else if (condition3) { // Runs if condition1 and condition2 are FALSE AND condition3 is TRUE } else { // Runs if ALL preceding conditions were FALSE }
Example: Grading System
javascript let grade = 85;
if (grade >= 90) { console.log('A'); } else if (grade >= 80) { console.log('B'); } else if (grade >= 70) { console.log('C'); } else { console.log('F'); }
// Output: B
Important: The conditions are checked sequentially. Once a condition is met, the corresponding block executes, and the entire if/else if/else structure is exited.