DEV Community

Cover image for Stop Writing Bad JavaScript! Master if, else, and switch in 5 Minutes
Kunal
Kunal

Posted on

Stop Writing Bad JavaScript! Master if, else, and switch in 5 Minutes

In our daily lives we make several decisions from deciding what to wear to deciding what to eat. In coding we have the same we call it Conditional Statements which is if , else and switch . In this Blog we will discuss about these Conditional statements.

Topics to Cover

  1. What control flow means in programming
  2. The if statement
  3. The if-else statement
  4. The else if ladder
  5. The switch statement
  6. When to use switch vs if-else

Control flow

In programming , the computer does not randomly runs the code. It follows a flow deciding what to do next based on the condition.

Think of a traffic signal

  • If the light is green - Go
  • If the light is yellow - Slow down
  • If the light is red - Stop!

Based on the condition you take the decision.

So , Control flow simply mean the order in which a program runs instruction and make decision.


The if statement

The if statement is used to run some code only when the condition is true

If statement consist of :

if

In programming we write like this :

const age = 20

if( age >= 18){
console.log("You can vote")
}
Enter fullscreen mode Exit fullscreen mode

It is run only when the condition is true

We can also use multiple if statement

const age = 18

if(age === 0) { console.log("You are just born!")}
if(age >= 0 && age < 18) { console.log("You are not eligible to vote")}
if(age >= 18) { console.log("You can vote")}

Enter fullscreen mode Exit fullscreen mode

Note:- The JavaScript engine checks every if statement one by one.
If a condition is true, its code runs.
Because of this, multiple if statements can run multiple if conditions which are true.


The if-else statement

Sometimes we want the program to do only one thing if the condition is true it runs the code otherwise it runs another thing if it is false.

This is where the if , else statement is used.

Think of ordering the food:

  • If the restaurant is open -> You can order the food
  • else -> You cook the food at home

In programming we write like this :

let isRestaurantOpen = true

if(isRestaurantOpen === true){
  console.log("You can order the food")
}else{
  console.log("You cook the food at home")
}

Enter fullscreen mode Exit fullscreen mode

Only one action will happen depending on the condition.


The else if ladder

Sometimes we need to check the multiple condition not just two
In this case we use else if ladder

In programming we write like this :

const marks = 82 

if (marks >= 90) {
  console.log("Grade A");
} else if (marks >= 75) {
  console.log("Grade B");
} else if (marks >= 50) {
  console.log("Grade C");
} else {
  console.log("Fail");
}
Enter fullscreen mode Exit fullscreen mode

Once a condition becomes true the rest condition are not checked


The switch statement

Sometimes we need to check only one variable against many possible values.
In this , we use the switch statement

It makes the code cleaner and easier to read then writing many else if condition

Lets think of you have your own restaurant in this :

  • If a user choose 1 -> Pizza
  • If a user choose 2 -> Burger
  • If a user choose 3 -> Pasta

You can also do this with else if ladder but we have better and easier alternate

In programming we can do this like :

const number = 1;

switch (number) {
  case 1:
    console.log("You ordered Pizza");
    break;

  case 2:
    console.log("You ordered Burger");
    break;

  case 3:
    console.log("You ordered Pasta");
    break;

  default:
    console.log("Please enter a valid number");
}

Enter fullscreen mode Exit fullscreen mode

Note : You have to add break to stop the condition after it matches with the result .
If you don't add the break statement the program will continue running to the next cases

Try running these examples on your computer to better understand how if-else and switch work.


When to use switch vs if-else

Point Use if-else Use switch
1 Used when checking ranges like age > 18 or marks > 50 Used when checking one variable against many fixed values
2 Used when conditions need multiple checks together Used when comparing one value with different cases
3 Better when there are few conditions Better when there are many options
4 Code can become long and harder to read with many conditions Code stays cleaner and more organized
5 Example: age check, marks grading, temperature check Example: day of week, menu selection, user role

Thanks for reading ! if enjoyed this blog , you can read more on this πŸ‘‡

Top comments (0)