01 / 20

If/Else Chains

Learn to control program flow with if, else if, and else statements: the building blocks of decision-making in JavaScript.

JavaScript

What Is Conditional Logic?

Programs often need to make decisions. Should we show a welcome message or an error? Should the character jump or duck? Conditional logic lets your code choose different paths based on whether something is true or false.

Start Condition? true Action A false Action B Continue

The Basic if Statement

An if statement checks a condition. If it is true, the code inside the curly braces runs. If it is false, the code is skipped entirely.

if (temperature > 100) { console.log("Water is boiling!"); }

Adding else: Two Paths

What if you want something to happen when the condition is false? The else block catches everything that the if does not.

if (age >= 18) { console.log("You can vote."); } else { console.log("Too young to vote."); }

The else if Chain

When you have more than two possible outcomes, chain multiple conditions together with else if. JavaScript checks each condition top to bottom and runs the first one that is true.

if (score >= 90) { grade = "A"; } else if (score >= 80) { grade = "B"; } else if (score >= 70) { grade = "C"; } else { grade = "F"; }

Anatomy of an Else-If Chain

score \(\geq\) 90?
true grade = "A" false
score \(\geq\) 80?
true grade = "B" false
score \(\geq\) 70?
true grade = "C" false grade = "F"

Chained vs. Separate if Statements

Chained (else if)
Only one block runs. As soon as a condition is true, the rest are skipped. Use when outcomes are mutually exclusive.
Separate ifs
Every if is checked independently. Multiple blocks can run. Use when conditions are independent of each other.
// Chained: only ONE runs if (x > 10) { console.log("big"); } else if (x > 5) { console.log("medium"); } // Separate: BOTH could run if (x > 10) { console.log("big"); } if (x > 5) { console.log("medium"); }

Writing Conditions

A condition is any expression that evaluates to true or false. JavaScript provides comparison and logical operators to build them.

Operator Meaning Example
===Strict equalityx === 5
!==Not equalx !== 0
\(>\) , \(<\)Greater / less thanage > 18
\(\geq\) , \(\leq\)Greater/less or equalscore >= 90
&&Logical ANDx > 0 && x < 10
||Logical ORday === "Sat" || day === "Sun"

How to Build an Else-If Chain

1
Identify all possible outcomes. List every distinct result your code needs to handle.
2
Order conditions from most specific to least. Put the narrowest or highest-priority check first.
3
Write the first if with its condition. This is the top gate of your chain.
4
Add else if for each middle condition. Each one is checked only if all above were false.
5
End with else as your safety net. It catches any value not handled above.

Example 1: Weather Advice

Given a temperature in Fahrenheit, print clothing advice.

let temp = 45; if (temp >= 85) { console.log("Wear shorts and sunscreen."); } else if (temp >= 60) { console.log("A light jacket is fine."); } else if (temp >= 32) { console.log("Bundle up, it's cold!"); } else { console.log("Stay inside, it's freezing!"); }

Example 2: Ticket Pricing

A movie theater charges different prices by age.

let age = 14; let price; if (age < 5) { price = 0; // Free } else if (age < 13) { price = 8; // Child } else if (age < 65) { price = 12; // Adult } else { price = 6; // Senior } console.log("Ticket: $" + price);

Example 3: Compound Conditions

You can combine conditions using && (AND) and || (OR) inside any branch.

let hour = 14; let isWeekend = true; if (hour < 6) { console.log("Sleeping..."); } else if (hour < 12) { console.log("Good morning!"); } else if (hour < 17 && isWeekend) { console.log("Enjoy your afternoon off!"); } else if (hour < 17) { console.log("Back to work."); } else { console.log("Good evening!"); }

Nesting vs. Chaining

Nested (avoid when possible)
An if inside another if. Harder to read and debug as depth grows.
Chained (preferred)
A flat else-if chain. Reads top-to-bottom like a checklist: clean and predictable.
// Nested (hard to follow) if (a) { if (b) { /* ... */ } else { /* ... */ } } else { /* ... */ } // Chained (clear) if (a && b) { /* ... */ } else if (a) { /* ... */ } else { /* ... */ }

Common Mistakes

✕ Using = instead of ===
if (x = 5) assigns 5 to x (always truthy). Use === for comparison.
✕ Wrong order of conditions
Putting a broad check first (e.g., \(\geq 70\) before \(\geq 90\)) means the specific branch never runs.
✕ Forgetting the else
Without a final else, unexpected inputs fall through silently with no output.

Quick Reference Table

Keyword When to Use How Many Per Chain?
if First condition check Exactly 1
else if Additional conditions after the first 0 or more
else Catch-all default when nothing above matched 0 or 1

Question 1 of 5

What does this code print?

Question 2 of 5

What is the purpose of the else block at the end of an if/else-if chain?

Question 3 of 5

What does this code output?

Question 4 of 5

A student writes if (score = 100) instead of if (score === 100). What happens?

Question 5 of 5

A student with grade 95 expects "Honor Roll" but gets "Pass". Why?