Control Flow in JavaScript

Welcome back, magical coders!
So far in our journey, we’ve learned how to store magical ingredients (variables) and how to cast reusable spells (functions). But right now, our code is a bit... predictable. It just runs straight from top to bottom, doing the exact same thing every single time.
But what if we want our code to think? What if we want it to make decisions based on what’s happening, just like the Sorting Hat at Hogwarts?
In programming, the ability to make decisions is called Control Flow. It’s how we tell our JavaScript to pick different paths. Today, we are going to learn the three most important decision-making spells: if, else, and switch.
Let’s put on the Sorting Hat and get started!
1. The if Statement: The Basic Question
The if statement is the simplest way to make a decision in code. It asks one simple question: "Is this true?"
If the answer is yes (true), the code runs. If the answer is no (false), the code just skips it and does nothing.
Imagine the Sorting Hat is sitting on a new student's head, and it’s looking for one specific trait: bravery.
JavaScript
let studentTrait = "bravery";
// The Hat asks: Is the student's trait bravery?
if (studentTrait === "bravery") {
// If YES, this magic happens!
console.log("You belong in Gryffindor!");
}
How it works:
We use the keyword
if.Inside the parentheses
(), we put our condition (our question). Here, we are checking ifstudentTraitis exactly equal to"bravery".Inside the curly braces
{}, we put the code we want to run only if the answer is true.
If studentTrait was "cunning" instead, the answer would be false. The code inside the curly braces would be completely ignored, and nothing would print to the screen.
2. The else Statement: The Fallback Plan (Plan B)
Asking just one question isn't always enough. What happens if the if condition is false, but we still want something to happen?
That’s where else comes in. It’s the backup plan. It means: "If the first thing is true, do this. Otherwise, do that."
Let's say the Sorting Hat only has two houses open today: Gryffindor and Hufflepuff.
JavaScript
let studentTrait = "loyalty";
if (studentTrait === "bravery") {
console.log("You belong in Gryffindor!");
} else {
// If they ARE NOT brave, this runs automatically as Plan B.
console.log("You belong in Hufflepuff!");
}
Because the student's trait is "loyalty", the if statement is false. So, JavaScript immediately skips the Gryffindor part and runs the else part. The else block doesn't need a question in parentheses—it’s the automatic catch-all for everything else.
3. The else if Statement: The Full Sorting Ceremony
Now we are ready for the real Sorting Ceremony. The Hat doesn't just check for one or two things; it checks a whole list!
When you have multiple different choices, you can chain them together using else if. JavaScript will check them one by one, from top to bottom, until it finds one that is true.
JavaScript
let studentTrait = "cunning";
if (studentTrait === "bravery") {
console.log("Gryffindor!");
} else if (studentTrait === "intelligence") {
console.log("Ravenclaw!");
} else if (studentTrait === "cunning") {
// JavaScript finds a match here! It runs this code and stops checking.
console.log("Slytherin!");
} else {
// If NONE of the above are true, this is the final backup.
console.log("Hufflepuff!");
}
The Golden Rule: As soon as JavaScript finds a condition that is true (like "cunning" above), it runs that specific block of code and then completely skips the rest. It won't even bother checking the else block at the end.
4. The switch Statement: The Organized Organizer
Using if and else if is great, but what if you have 10 or 20 different things to check? Writing else if over and over again gets very messy to read.
When you are checking a single variable against a bunch of specific, exact values, the switch statement is your best friend. It’s like an incredibly organized magical filing cabinet.
Let's say we have a magic door that requires a specific password to open a specific room.
JavaScript
let password = "Caput Draconis";
switch (password) {
case "Caput Draconis":
console.log("Welcome to the Gryffindor Common Room.");
break;
case "Pure-blood":
console.log("Welcome to the Slytherin Common Room.");
break;
case "Pig Snout":
console.log("Password outdated. Access Denied.");
break;
default:
console.log("Incorrect password. The door remains shut.");
}
Let's break down this organized magic:
switch (password): We tell JavaScript, "Hey, look at thepasswordvariable."case "..." :This is like asking, "Does it match this exact label?"break;(SUPER IMPORTANT): This is the magic stop sign! Once JavaScript finds a match and runs the code, thebreaktells it to exit theswitchcompletely. If you forget to writebreak;, JavaScript will accidentally run the code for the next rooms too! (Imagine opening the Gryffindor door and accidentally opening the Slytherin door at the same time—disaster!).default:This is exactly like theelseblock. It’s the backup plan. If the password doesn't match any of the cases, thedefaultcode runs.
Summary: Which Spell Should You Use?
Use
ifandelsewhen you have just one or two simple decisions to make, or when you are checking things like "greater than" or "less than" (e.g.,if (age >= 11)).Use
else ifwhen you have a few specific conditions to check in a row.Use
switchwhen you have a long list of exact matches to check against a single variable. It keeps your code looking clean and readable!
Programming is all about logic and choices. Now that you understand Control Flow, your JavaScript can start making intelligent decisions just like the Sorting Hat!
