Skip to main content

Command Palette

Search for a command to run...

Control Flow in JavaScript: If, Else, and Switch

Updated
4 min read
Control Flow in JavaScript: If, Else, and Switch
D
CS student focused on backend & OS fundamentals. Building real projects and sharing practical learning.

what do u understand by control flow - basically the order in which code runs , by default JS run code line by line , we want control on these thing . you dont need to gave that much time on this topic just know basic - basic stuff and all set.

Example:

  • If age is above 18 → allow

  • If marks are high → show pass

  • If day is Monday → show message

That decision-making system is called control flow.

lets understand different type of statement

1. The if Statement

Runs code only when the condition is true.

Syntax:

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

Example: Check Age

let age = 20;

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

How it works:

  • JavaScript checks the condition.

  • If true → code runs.

  • If false → nothing happens.


2. The if-else Statement

Provides two options:

  • One for true

  • One for false


Example:

let age = 16;

if (age >= 18) {
  console.log("You can vote.");
} else {
  console.log("You cannot vote.");
}

How it works step-by-step:

  1. Check condition.

  2. If true → first block runs.

  3. If false → else block runs.

Only one block executes.


3. The else if Ladder

Used when we have multiple conditions.

Example: Grade System

let marks = 85;

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

How it runs:

  • Checks first condition.

  • If false → moves to next.

  • Stops at first true condition.

  • If none match → else runs.

This is called a decision ladder.


4. The switch Statement

Used when checking one variable against multiple values.

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;

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

Important: break

Without break, execution continues to next cases.

This is called fall-through.

Example of wrong behavior:

let day = 1;

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

Both will print.

That’s why break is necessary.


When to Use switch vs if-else

Use if-else when:

  • Conditions are complex

  • Using ranges (>, <, >=)

  • Multiple different expressions

Example:

if (marks > 90 && age > 18)

Use switch when:

  • Checking one variable

  • Comparing exact values

  • Clean menu-style logic

Example:

  • Day numbers

  • Menu options

  • Status codes


now lets do some brain storming

  1. Write a program that checks:

    • If a number is positive, negative, or zero

let number = 5;

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

Why used if-else?

Because we are checking wide range condition, not fixed values.

2. Write a program that prints the day of the week using switch

let day = 2;

switch (day) {
  case 1:
    console.log("Sunday");
    break;
  case 2:
    console.log("Monday");
    break;
  case 3:
    console.log("Tuesday");
    break;
  case 4:
    console.log("Wednesday");
    break;
  case 5:
    console.log("Thursday");
    break;
  case 6:
    console.log("Friday");
    break;
  case 7:
    console.log("Saturday");
    break;
  default:
    console.log("Invalid input");
}

Why used switch?

Because we are comparing one variable (day) with fixed values.