Skip to main content

Command Palette

Search for a command to run...

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

Published
4 min read
Control Flow in JavaScript: If, Else, and Switch Explained

When writing programs, we often need the computer to make decisions.

For example:

  • If it is raining → take an umbrella

  • If a student scores above 40 → they pass

  • If today is Sunday → take a holiday

This type of decision-making in programming is called control flow.

Control flow allows our program to choose which code should run based on conditions.

In JavaScript, the most common control flow tools are:

  • if

  • if-else

  • else if

  • switch

Let’s understand how each one works.

What Control Flow Means in Programming

Control flow determines the order in which statements are executed in a program.

Normally, code runs line by line from top to bottom.

Example:

let number = 5;

console.log("Start");
console.log(number);
console.log("End");

Output:

Start
5
End

But sometimes we want the program to run code only when a condition is true.

That’s where control flow statements come in.

The if Statement

The if statement runs a block of code only if a condition is true.

Syntax :

if (condition) {
  // code runs if condition is true
}

Example: Checking Age

let age = 20;

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

Output:

You are eligible to vote.

Step by step:

  1. JavaScript checks the condition age >= 18

  2. If it is true, the code inside {} runs

  3. If it is false, nothing happens


The if-else Statement

Sometimes we want two possible outcomes.

Example:

  • If a student passes → show "Passed"

  • Otherwise → show "Failed"

We use if-else.

Syntax :

if (condition) {
  // runs if true
} else {
  // runs if false
}

Example: Pass or Fail

let marks = 35;

if (marks >= 40) {
  console.log("You passed the exam.");
} else {
  console.log("You failed the exam.");
}

Output:

You failed the exam.

The else if Ladder

Sometimes we need multiple conditions, not just two.

Example:

  • Marks above 90 → Grade A

  • Marks above 75 → Grade B

  • Marks above 50 → Grade C

  • Otherwise → Fail

For this we use else if.

Syntax

if (condition1) {
}
else if (condition2) {
}
else if (condition3) {
}
else {
}

Example: Student Grades

let 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");
}

Output:

Grade B

Step by step:

  1. Check first condition

  2. If false → check next

  3. Continue until one is true

Only one block runs.

The switch Statement

The switch statement is another way to make decisions when comparing one value against many options.

It works well when you have many fixed cases.

Syntax

switch (value) {
  case option1:
    // code
    break;

  case option2:
    // code
    break;

  default:
    // code
}

Example: Day of the Week

let day = 3;

switch(day) {
  case 1:
    console.log("Monday");
    break;

  case 2:
    console.log("Tuesday");
    break;

  case 3:
    console.log("Wednesday");
    break;

  case 4:
    console.log("Thursday");
    break;

  case 5:
    console.log("Friday");
    break;

  case 6:
    console.log("Saturday");
    break;

  case 7:
    console.log("Sunday");
    break;

  default:
    console.log("Invalid day");
}

Output:

Wednesday

Why break Is Important in switch

The break statement stops the switch from continuing to the next case.

Without break, JavaScript continues executing the next cases.

Example without break:

let day = 1;

switch(day) {
  case 1:
    console.log("Monday");
  case 2:
    console.log("Tuesday");
}

Output:

Monday
Tuesday

Because there was no break, both cases ran.

So remember:

Always use break in switch cases

When to Use switch vs if-else

Both are useful, but they work best in different situations.

Use Case Best Choice
Range comparisons (marks, age) if / else if
Many exact values switch
Complex conditions if-else
Fixed menu options switch

Example:

Checking marks range → use if-else

Checking day number → use switch

JS if else switch case