Skip to main content

Command Palette

Search for a command to run...

Control Flow in JavaScript: If, Else and Switch Explained

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

When we write programs, not every line of code should run every time.

Sometimes the program needs to make decisions.

Think about a game.

  • If a player has enough health, they continue playing.

  • If the health reaches zero, the game is over.

  • If the player collects a special item, they unlock a new ability.

The game constantly checks conditions and decides what should happen next.

This decision making is called control flow.

Control flow controls which part of the program runs based on conditions.

In JavaScript we mainly use:

  • if

  • if-else

  • else if

  • switch

Let’s understand them using simple game examples.


The if Statement

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

Imagine a player trying to enter a special level that requires at least 100 coins.

let coins = 120

if (coins >= 100) {
  console.log("Level unlocked")
}

Output:

Level unlocked

If the player has enough coins, the level unlocks.

If not, nothing happens.


The if-else Statement

Sometimes we want two possible outcomes.

Example: checking player health.

let health = 0

if (health > 0) {
  console.log("Player is still alive")
} else {
  console.log("Game Over")
}

Output:

Game Over

Here the game checks the condition.

If true → player continues
If false → game ends


The else if Ladder

Games often have multiple conditions.

Example: assigning player rank based on score.

let score = 850

if (score >= 1000) {
  console.log("Legend Rank")
}
else if (score >= 800) {
  console.log("Diamond Rank")
}
else if (score >= 500) {
  console.log("Gold Rank")
}
else {
  console.log("Bronze Rank")
}

Output:

Diamond Rank

The program checks conditions from top to bottom.

As soon as one condition is true, the rest are skipped.


The switch Statement

Sometimes we check specific values instead of ranges.

Example: choosing a character class in a game.

let character = "mage"

switch (character) {
  case "warrior":
    console.log("Strong attack and high defense")
    break

  case "mage":
    console.log("Powerful magic abilities")
    break

  case "archer":
    console.log("Long range attacks")
    break

  default:
    console.log("Unknown character")
}

Output:

Powerful magic abilities

The break statement stops execution once a match is found.

Without break, the next cases would also run.


When to Use switch vs if-else

Both structures help us make decisions.

Use if-else when checking conditions or ranges.

Example:

score >= 800
health > 0
coins >= 100

Use switch when checking specific values.

Example:

character type
weapon type
game difficulty

Examples

Check if a player's score is positive, negative or zero.

let score = -10

if (score > 0) {
  console.log("Positive score")
}
else if (score < 0) {
  console.log("Negative score")
}
else {
  console.log("Score is zero")
}

Now choose a game difficulty using switch.

let difficulty = "hard"

switch (difficulty) {
  case "easy":
    console.log("Enemies are weaker")
    break

  case "medium":
    console.log("Balanced difficulty")
    break

  case "hard":
    console.log("Enemies are stronger")
    break

  default:
    console.log("Unknown difficulty")
}

In the first example we used if-else because we are checking conditions.

In the second example we used switch because we are checking specific values.


Control flow is one of the most important concepts in programming.

It allows programs to react to different situations.