Skip to main content

Command Palette

Search for a command to run...

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

Updated
•3 min read•View as Markdown

We make decisions all the time. If it's raining, take an umbrella. If you're hungry, eat something. Programming works the same way—code doesn't just run top to bottom blindly. It evaluates conditions and chooses different paths. That's control flow.

The "if" Statement

The simplest decision in JavaScript is the if statement. It checks a condition and runs code only if that condition is true:

const age = 18;

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

The condition (age >= 18) is evaluated. If true, the code inside the curly braces runs. If false, it skips.

Think of it like a gate—only passes through if the condition passes.

The "if-else" Statement

Sometimes you want to do one thing when true, another when false:

const score = 75;

if (score >= 60) {
  console.log("You passed!");
} else {
  console.log("Try again.");
}

If the condition is true, the first block runs. Otherwise, the else block runs.

The "else if" Ladder

When you have multiple conditions to check, use else if:

const marks = 85;

if (marks >= 90) {
  console.log("Grade: A");
} else if (marks >= 80) {
  console.log("Grade: B");
} else if (marks >= 70) {
  console.log("Grade: C");
} else if (marks >= 60) {
  console.log("Grade: D");
} else {
  console.log("Grade: F");
}

JavaScript checks each condition in order. Once one is true, it runs that block and skips the rest.

The "switch" Statement

When you have many discrete values to compare, switch is cleaner:

const 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;
  default:
    console.log("Weekend!");
}

The break is important—without it, execution "falls through" to the next case. That's usually not what you want.

When to Use Which

Use Case Best Choice
True/false check if-else
Multiple ranges or conditions else if ladder
Many discrete values switch

If you're checking 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");
}

For the day of the week, switch makes more sense because you're matching exact values:

function getDayName(dayNumber) {
  switch (dayNumber) {
    case 1: return "Sunday";
    case 2: return "Monday";
    case 3: return "Tuesday";
    case 4: return "Wednesday";
    case 5: return "Thursday";
    case 6: return "Friday";
    case 7: return "Saturday";
    default: return "Invalid";
  }
}

The choice depends on what you're comparing. Ranges and boolean conditions work well with if. Exact value matching works well with switch.

Wrapping Up

Control flow is about making decisions in your code. if checks conditions, else if handles multiple conditions, and switch handles many discrete values cleanly. Pick the right tool for the situation—your code will be clearer and easier to follow.