Skip to main content

Command Palette

Search for a command to run...

Control Flow in JavaScript

If, Else, and Switch Explained

Updated
Control Flow in JavaScript

Every program you write is not just about running code. It is about making decisions.

Think about real life. Every day, you make choices.

If it is raining, you take an umbrella.
If you are hungry, you eat.
If it is late, you go to sleep.

Your actions depend on conditions.

Programming works the same way.

This is exactly what control flow is.


What Control Flow Means

Control flow is the order in which your code executes based on conditions.

Instead of running everything line by line, your program decides what to run and what to skip.

Without control flow, your program would behave the same way every time, regardless of input.

That would make it useless for real-world applications.


The if Statement

The simplest way to introduce decision-making in your code is the if statement.

It checks a condition and runs code only if that condition is true.

const age = 20;

if (age >= 18) {
  console.log("You are eligible to vote");
}

Here is how it works step by step.

First, the condition age >= 18 is evaluated.

If it is true, the code inside the block runs.

If it is false, nothing happens.

This is the most basic form of control flow.


The if-else Statement

In real-world scenarios, you usually want to handle both outcomes.

That is where else comes in.

const age = 16;

if (age >= 18) {
  console.log("Eligible");
} else {
  console.log("Not eligible");
}

Now the program does not just check a condition. It reacts differently depending on the result.

If the condition is true, the first block runs.

If it is false, the second block runs.


The else if Ladder

Sometimes you have more than two possible outcomes.

For example, grading a student.

const marks = 75;

if (marks >= 90) {
  console.log("A Grade");
} else if (marks >= 70) {
  console.log("B Grade");
} else if (marks >= 50) {
  console.log("C Grade");
} else {
  console.log("Fail");
}

This is called an else-if ladder.

The program checks conditions from top to bottom.

The moment it finds a true condition, it executes that block and stops checking further.

Understanding this flow is important.

Only one block runs, even if multiple conditions are true.


The switch Statement

Now consider a different kind of problem.

You are not checking ranges. You are checking exact values.

For example, days of the week.

const day = "Monday";

switch (day) {
  case "Monday":
    console.log("Start of the week");
    break;
  case "Friday":
    console.log("Almost weekend");
    break;
  default:
    console.log("Regular day");
}

Here, the value of day is compared with each case.

When a match is found, the corresponding block runs.


Why break is Important

The break statement stops the execution of the switch.

Without it, the program continues to execute the next cases even if they do not match.

const day = "Monday";

switch (day) {
  case "Monday":
    console.log("Start");
  case "Tuesday":
    console.log("Next day");
}

Output:

Start
Next day

This behavior is called fall-through.

In most cases, you do not want this, which is why break is important.


When to Use switch vs if-else

Choosing the right structure depends on the problem.

Use if-else when you are working with conditions like:

  • ranges

  • multiple logical checks

Use switch when:

  • you are comparing a single variable

  • there are fixed known values

For example:

  • checking marks → use if-else

  • checking day or role → use switch


How Code Actually Flows

You can think of control flow like a decision tree.

Condition → True → Execute Block A  
          → False → Execute Block B

For switch:

Value → Match Case 1  
      → Match Case 2  
      → Default

This helps you visualize how your program moves.


Putting It All Together

Let’s solve a simple real-world problem.

Check if a number is positive, negative, or zero

const num = -5;

if (num > 0) {
  console.log("Positive");
} else if (num < 0) {
  console.log("Negative");
} else {
  console.log("Zero");
}

Here, we used if-else because we are checking conditions.


const day = "Tuesday";

switch (day) {
  case "Monday":
    console.log("Start of week");
    break;
  case "Tuesday":
    console.log("Second day");
    break;
  case "Sunday":
    console.log("Weekend");
    break;
  default:
    console.log("Other day");
}

Here, we used switch because we are matching exact values.


Final Thought

Control flow is what makes your code intelligent.

It allows your program to make decisions instead of blindly executing instructions.

Once you understand how if, else, and switch work, you can start building logic that behaves differently based on input.

And that is where programming starts becoming powerful.