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

* * *

## What Control Flow Means in Programming

Control flow is simply the order in which your code executes.

By default, JavaScript runs code top to bottom, line by line. But real-world logic isn’t always that straightforward.

Sometimes you want,

*   “If this happens, do this”
    
*   “Otherwise, do something else”
    

That decision-making ability is what control flow gives you.

* * *

## The `if` Statement

The `if` statement is the most basic decision-maker.

It runs a block of code only if a condition is true.

```javascript
let age = 18;

if (age >= 18) {
  console.log("You are an adult");
}
```

If the condition is `true`, the code runs.

If it's `false`, JavaScript simply skips it.

* * *

## The `if-else` Statement

What if you want to handle both outcomes?

That’s where `else` comes in.

```javascript
let age = 16;

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

`if` handles the true case

`else` handles everything else

* * *

## The `else if` Ladder

**Sometimes you have multiple conditions, not just two.**

```javascript
let score = 75;

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

JavaScript checks conditions top to bottom

The first true condition wins, and the rest are ignored

* * *

## The `switch` Statement

Think of `switch` like a menu selector.

You have one value and you want to check it against many exact options—not ranges, not complex logic—just direct matches.

Imagine this:

**“Based on this one value, which exact case should run?”**

That’s exactly what `switch` does.

```javascript
let role = "admin";

switch (role) {
  case "admin":
    console.log("Full access");
    break;
  case "user":
    console.log("Limited access");
    break;
  case "guest":
    console.log("View only");
    break;
  default:
    console.log("Unknown role");
}
```

### What’s Happening Here?

*   JavaScript checks:  
    Is `role === "admin"`? --> Yes --> run that block
    
*   Then it stops immediately because of `break`
    

If there was no match:

*   It would go to `default` (like `else`)
    

### Why `break` is Important

Without `break`, JavaScript keeps running the next cases (this is called fall-through).

```javascript
let role = "admin";

switch (role) {
  case "admin":
    console.log("Admin panel");
  case "user":
    console.log("User panel");
}

//Output: 
//Admin panel
//User panel
```

That’s usually not what you want.

### When It Feels More Natural Than `if-else`

Instead of writing

```javascript
if (role === "admin") { ... }
else if (role === "user") { ... }
else if (role === "guest") { ... }
```

You write it once:

```javascript
switch (role) { ... }
```

*   Cleaner
    
*   Easier to scanBetter when values are fixed
    

* * *

## When to Use `switch` vs `if-else`

Here’s the practical difference:

### Use `if-else` when :

*   You’re dealing with ranges or conditions
    
*   Logic involves comparisons (`>`, `<`, `!==`, etc.)
    

Example: checking marks, age, validation rules

* * *

### Use `switch` when:

*   You’re comparing one variable to fixed values
    
*   You want cleaner, more readable code
    

Example: menu options, roles, days of the week

* * *

## Conclusion

Control flow is what makes your code feel **smart** instead of static.

*   `if` → simple decisions
    
*   `if-else` → two outcomes
    
*   `else if` → multiple conditions
    
*   `switch` → clean handling of fixed values
    

* * *
