Control Flow in Js: If, Else & switch

Hello readers....
I hope you guys are enjoying this blog series of JS, and trust me, I'm enjoying it too.
Till now, I have covered two topics of js first one is Variables & second is Operators. Both are easy, but in JS, there are some interesting terms that I have covered in my blog. If you have not read it yet, read that first.
In this blog, I'm going to tell you about the control flow. Sounds easy, right? But you came to my blog, so as always, I will make things interesting and mind-blowing for you.
Let's start
Control Flow
Every day we make decisions.
If one feature is completed -----> commit
If it's 2 AM and you're feeling hungry -----> Make Maggie
If your battery is low ------> charge your phone.
And many more...
That's exactly how decision-making also works in programming.
You control the flow of your code by making a decision based on the input you get.
Why is control flow important?
Imagine you are going somewhere and you decide to turn right at a junction. Later, you realize that the decision was wrong.
Now you're lost.
Maybe you're late.
Maybe you reached the wrong place.
Maybe you missed something important. One small decision… and everything changes. I don’t think I need to explain further — you can easily imagine the trouble a wrong decision can cause.
Now imagine the same thing happening inside a program. Programs also need to make decisions. If they don’t, or if they make the wrong decision, the consequences can be serious.
In programming, also, just imagine
If an ambulance is not called at the emergency, it is just because of the control flow.
Imagine you are playing a quiz online and the question setter marked the wrong answer as true. You will get 0 marks for your right answer, which can pull you down from others.
Imagine a login system that doesn't check credentials and logs you in. It is totally a security disaster.
And many more situations like this exist. So it’s not just about making decisions, it’s about making the right decisions.
The same applies to software. When we write programs, we are not just writing instructions — we are designing decision-making systems.
Sometimes the result of a wrong condition might be small, like showing a wrong message on a website.
But sometimes the consequences can be serious. Because in many cases, human lives depend on software.
A wrong decision in code might mean:
a failed emergency response
a financial transaction error
a system allowing unauthorized access
Ways to control the flow
If
If.... else
if else ladder
Switch
These are also known as conditional statements.
The if Statement
The if Statement runs the code only if the condition is true.
For example, if your age is greater than or equal to 18 then you can vote.
The Js version
const age = 18;
if(age >= 18){
console.log("you can vote")
}
That's it.
Here is one more example
let orderAmount = 230
if(orderAmount > 199) console.log("Free Delivery")
The if...else statement
In the above example, I have written if orderAmount is greater than 199 then free delivery, what if it is less than that case can be handled using the else statement.
let orderAmount = 195
if(orderAmount > 199){
console.log("Free Delivery")
}else{
console.log(`add item worth ${199-orderAmount} to get free delivery`)
}
So, it is recommended to use else show that the user can get feedback or know what they are doing wrong.
Here is one more example:
let isPasswordCorrect = true;
if(isPasswordCorrect) console.log("you are logged in")
else console.log("Password is not correct")
The if...else ladder
In real life descision often have many possibilities. For example
| Marks | Grade |
|---|---|
| 90+ | A |
| 80+ | B |
| 60+ | C |
| Below 60 | Fail |
In this case, we use if else ladder. Let's understand with the code.
const marks = 82;
if (marks > 90){console.log("Grade A");}else if(marks > 80){
console.log("Grade B");}else if(marks > 60){
console.log("Grade C");}else
{
console.log("Fail")
}
Things you should remember while using the if-else ladder:
JavaScript runs this ladder from top to bottom always.
It returns as the first test case passed, always use higher to lower comparison
You can also use the break keyword to break at some point.
The switch statement
Can you see the problem with if else ladder? Yup, you are correct, if there are multiple values, then it will go long and hard to read and maintain.
So, a switch is the solution to the above problem.
A switch is used when you have to check a single variable with multiple values.
Here is an example:
const orderStatus = "Out for delivery";
switch (orderStatus) {
case "Preparing":
console.log("Restaurant is preparing your food 🍳");
break;
case "Out for delivery":
console.log("Your food is on the way 🚴");
break;
case "Delivered":
console.log("Enjoy your meal 🍔");
break;
default:
console.log("Order status unknown");
}
In the switch case break statement is very important. If you don't write this, then js will run you all the test cases.
So when you write break them once the case is true, then due to the break statement, you return from the switch statement, and no other case runs.
Also, the default is recommended. It ensures that in case of no match, it gives output to the user.
Well, that's it about the if...else, if else ladder and switch statement.
Now guess the output of this code:
if ("0")console.log("this will print")
if("") console.log("Empty string prints")
It is quite confusing, right? That's why you must know the truth and falsity values to efficiently use conditional statements.
Falsy values
Here is the list of falsy values in JS
false
0
0n
-0
"" (Empty string)
null
undefined
NaN
Other than these, all are truthy values.
Values like this, which look falsy but are truthy.
"0"
"false"
[]
{}
That's it for this blog. I hope it cleared all your doubts related to conditional statements in JavaScript.
If you liked the writing and found the blog helpful, do share it and let me know your feedback.
I’ll see you in the next blog with more in-depth learning and some mind-blowing JavaScript concepts. 🚀





