Quest 1 • Lesson 4
🔀 JavaScript Conditional Statements
Make your code think. Learn how to run different blocks of code based on conditions using if, else, and switch.
Conditional statements let your program make decisions. Based on whether a condition is true or false, different code runs.
1️⃣ The if Statement
Run a block of code only if a condition is true.
const age = 20;
if (age >= 18) {
console.log("You can vote!");
}
if— starts the block(age >= 18)— the condition (in parentheses){ }— the code to run if true
2️⃣ if / else — Two Paths
Add an else block to handle the "false" case.
const temperature = 30;
if (temperature > 25) {
console.log("It's hot outside");
} else {
console.log("It's cool outside");
}
💡 One and only one branch runs. If temperature > 25 is true, only the first block runs. Otherwise only the second.
3️⃣ if / else if / else — Multiple Paths
Chain conditions together with else if. JavaScript checks them top to bottom and stops at the first true one.
const score = 85;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}
// Output: Grade: B (because 85 >= 80)
💡 Order matters. Put the most specific condition first. If you checked score >= 70 first, everything above 70 would return "C".
🎚️ Interactive: Grade Calculator
Slide to change the score and see the conditional logic run live.
4️⃣ The switch Statement
When you're comparing the same value against many specific cases, switch is cleaner than a chain of else if.
const day = "Wednesday";
switch (day) {
case "Monday":
console.log("Start of the week");
break;
case "Wednesday":
console.log("Midweek!");
break;
case "Friday":
console.log("Almost weekend");
break;
default:
console.log("Just another day");
}
// Output: Midweek!
⚠️ Don't forget break. Without it, execution "falls through" and runs every case below the match. This is a common beginner bug.
🚦 Interactive: Traffic Light Logic
Click a light to see what JavaScript decides. Uses a switch statement.
5️⃣ Ternary Operator — Short if/else
For simple true/false decisions, use the ternary operator: condition ? ifTrue : ifFalse.
const age = 20;
const status = age >= 18 ? "Adult" : "Minor";
console.log(status); // "Adult"
// Same as:
let status2;
if (age >= 18) {
status2 = "Adult";
} else {
status2 = "Minor";
}
💡 When to use ternary: only for short, simple cases. If the logic gets complex, use if/else — it's easier to read.
🚀 Try It Live
Edit the code below and click Run.
✨ Challenge: Temperature Advisor
Write a conditional that prints advice based on a temperature value:
- Below 0 → "Freezing! Wear a heavy coat."
- 0 to 15 → "Cold. Bring a jacket."
- 16 to 25 → "Pleasant. Light clothing."
- Above 25 → "Hot! Stay hydrated."
📚 What's Next?
- Lesson 1.5: Loops → — repeat code with for and while.
- ← Previous: Lesson 1.3 Operators
- ⚡ JavaScript Course Hub →
❤️ Support Free Education
This course is 100% free. If it helps you, consider buying me a coffee.
☕ Buy Me a Coffee