Control Flow in JavaScript: If, Else, and Switch Explained

Search for a command to run...

No comments yet. Be the first to comment.
When writing JavaScript code, you often need to perform calculations, compare values, or combine conditions. To do these tasks, JavaScript uses something called operators. If you’ve ever done basic ma

When learning JavaScript, you’ll eventually encounter a keyword that can feel confusing at first: this. The value of this changes depending on how a function is called. JavaScript also provides specia

Functions are one of the most important concepts in JavaScript. They help you organize your code and avoid repeating the same logic again and again. In JavaScript, there are multiple ways to create fu

When writing JavaScript programs, you often need to store multiple values together. For example, imagine you want to store a list of fruits: Apple Mango Banana Orange You could store them like t

As JavaScript applications grow, managing code and data can become complicated. To make programs easier to organize and reuse, developers often use a programming style called Object-Oriented Programmi

ygshjm
24 posts
life is basically just one long string of decisions. if it’s raining, you grab an umbrella. if you’re tired, you drink chai. if your battery hits 5%, you panic and look for a charger.
programming works the exact same way. by default, code is a bit "dumb"—it just runs from top to bottom like a waterfall.
but real apps need to make choices.
that’s where control flow comes in. it’s the steering wheel that tells your code which path to take.
why did control flow even come?
without control flow, your code would be a mindless robot. imagine an instagram where the "delete" button worked for everyone, even if they didn't own the post. or an atm that gave out cash even if you had zero balance.
we need code that can ask a question ("is this user logged in?") and act differently based on the answer. it's what makes an app feel "smart" and interactive.
if statement: the "only if" rulethis is the simplest version of a decision. it’s like telling a friend: "if you pass the bakery, buy some cookies." if they don't pass the bakery, they do nothing and just come home.
how to use it:
let age = 20;
if (age >= 18) {
console.log("you’re officially an adult. congrats!");
}
if-else statement: the "fork in the road"in the real world, we usually have a backup plan. if the first thing isn't true, we do the second thing.
let marks = 35;
if (marks >= 40) {
console.log("you passed!");
} else {
console.log("time for a re-test.");
}
else if ladder: the "multiple choices"sometimes life isn't just "yes" or "no." sometimes you have a bunch of options, like a traffic light or grading a student.
let signal = "yellow";
if (signal === "red") {
console.log("stop right there!");
} else if (signal === "yellow") {
console.log("slow down and prepare to stop.");
} else if (signal === "green") {
console.log("go go go!");
} else {
console.log("wait, that’s not a normal signal...");
}
why it’s important: it prevents the computer from checking every single condition. once it finds that the signal is "yellow," it stops looking and ignores the rest of the ladder. it saves energy!
switch statement: the "organized menu"imagine you are at a restaurant with 10 different pizza options. using an else if ladder for 10 items would look like a giant, messy wall of text.
switch is a cleaner way to handle many specific "cases." it’s like a vending machine—you press button #2, and you get the item for #2.
let day = 2;
switch (day) {
case 1:
console.log("it's monday. back to work.");
break;
case 2:
console.log("it's tuesday. getting into the groove.");
break;
case 3:
console.log("it's wednesday. hump day!");
break;
default:
console.log("it's just another day.");
}
breakthink of break like the exit door of a shop. without it, once javascript finds a match, it will keep running every single case below it (this is called "falling through").
if you forgot the break on case 2, it would print "it's tuesday" and "it's wednesday." always remember to shut the door!