Back to blog
JavaScript

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

May 10, 2026 3 min read

What Is Control Flow?

Control flow decides which code should run based on conditions.

Real-life example:

If it rains:
  take umbrella
Else:
  go normally

Programming works similarly.


The if Statement

Runs code only if a condition is true.


Syntax

if (condition) {
  // code
}


Example

const age = 20;

if (age >= 18) {
  console.log("Adult");
}


Output

Adult


How if Works

Diagram: flowchart TD

The if-else Statement

Runs one block if condition is true, otherwise another block.


Example

const age = 15;

if (age >= 18) {
  console.log("Adult");
} else {
  console.log("Minor");
}


Output

Minor


Flowchart

Diagram: flowchart TD

The else if Ladder

Used when multiple conditions exist.


Example

const marks = 75;

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


Output

Grade B


Step-by-Step Execution

marks >= 90 → false
marks >= 70 → true
Print "Grade B"
Stop checking further


The switch Statement

Used when comparing one value against many options.


Syntax

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

  case option2:
    // code
    break;

  default:
    // code
}


Example

const day = 2;

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

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

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

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


Output

Tuesday


Switch-Case Branching Diagram

Diagram: flowchart TD

Why break Is Important

break stops execution after a matching case.

Without break, execution continues to next cases.


Example Without break

const day = 1;

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

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


Output

Monday
Tuesday


When to Use switch vs if-else

Use CaseRecommended
Range conditionsif-else
Multiple exact valuesswitch
Complex logicif-else
Menu/day selectionswitch

Example: if-else Better Choice

const temperature = 35;

if (temperature > 40) {
  console.log("Very Hot");
} else if (temperature > 30) {
  console.log("Hot");
}

Ranges are easier with if-else.


Example: switch Better Choice

const role = "admin";

switch (role) {
  case "admin":
    console.log("Full Access");
    break;

  case "editor":
    console.log("Edit Access");
    break;
}

Exact values are cleaner with switch.


Assignment 1

Check Positive, Negative, or Zero

const number = -5;

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


Why if-else Was Used

Because we are checking multiple conditions and ranges.


Assignment 2

Print Day Using switch

const day = 3;

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

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

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

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


Why switch Was Used

Because one variable is compared against multiple exact values.


Key Takeaways

ConceptSummary
ifRuns code if condition is true
if-elseChooses between two paths
else ifHandles multiple conditions
switchCompares one value to many cases
breakStops switch execution

Final Notes

Control flow is fundamental in programming because it allows applications to make decisions.

Used everywhere:

  • Authentication

  • Form validation

  • Role permissions

  • API logic

  • Game logic

  • UI rendering


0 Comments

Sign in to join the conversation

No comments yet. Be the first to comment!