Skip to main content

Command Palette

Search for a command to run...

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

Control flow in Javascript for conditional statements

Updated
β€’4 min readβ€’View as Markdown
Control Flow in JavaScript: If, Else, and Switch Explained

Control flow simply means:

πŸ‘‰ The order in which your code executes (runs). By default, JavaScript runs code line by line (top β†’ bottom).

Here’s a clear step-by-step flow breakdown for each control statement β€” think like a flowchart in words πŸ‘‡

  1. if Statement (Single Decision)

    🧠 Flow Steps:
    -> Start
    -> Check condition
        πŸ‘‰ If true β†’ execute block
        πŸ‘‰ If false β†’ skip block
    -> End
    
    if (condition) {
        // runs only if true
    }
    
    πŸ“Š Flow Idea

    Start β†’ Condition β†’ (True β†’ Run Code β†’ End) (False β†’ End)

  2. if - else Statement (Two Paths)

    🧠 Flow Steps:
    -> Start
    -> Check condition
        πŸ‘‰ If true β†’ run if block
        πŸ‘‰ If false β†’ run else block
    -> End
    
    if (condition) {
        // if block
    } else {
        // else block
    }
    
    πŸ“Š Flow Idea

    Start β†’ Condition

    β”œβ”€β”€ True β†’ IF block β†’ End

    └── False β†’ ELSE block β†’ End

  3. if - else if - else (Multiple Conditions)

    🧠 Flow Steps:
    -> Start
    -> Check first condition
        πŸ‘‰ If true β†’ execute block β†’ End
        πŸ‘‰ Else β†’ check next condition
    -> Repeat until:
        πŸ‘‰ One condition is true β†’ run block β†’ End
        πŸ‘‰ None true β†’ run else block
    -> End
    
    if (cond1) {
        // block 1
    } else if (cond2) {
        // block 2
    } else {
        // default block
    }
    
    πŸ“Š Flow Idea

    Start β†’ cond1? β”œβ”€β”€ True β†’ Block1 β†’ End

    └── False β†’ cond2?

    β”œβ”€β”€ True β†’ Block2 β†’ End

    └── False β†’ ELSE β†’ End

  4. switch Statement (Multi-Branch Matching)

    🧠 Flow Steps:
    -> Start
    -> Evaluate expression
    -> Compare with each case
        πŸ‘‰ If match found:
            -> Execute that case
            -> Stop at break
        πŸ‘‰ If no match β†’ run default
    -> End
    
    switch (value) {
        case 1:
            // code
            break;
        case 2:
            // code
            break;
        default:
            // fallback
    }
    

🧠 Final Mental Model

Statement Type of Flow Use Case
if Single decision One condition
if-else Two-way decision Either A or B
if-elseif Multi conditions Range/complex logic
switch Multi matching Fixed values

🧾 Final Verdict

Factor if-else switch
Speed O(n) O(1) / O(n)
Flexibility βœ… High ❌ Limited
Readability Medium High (many cases)
Best Use Logic Fixed values

πŸ“Š Real Performance Comparison

  1. Few Conditions (≀ 5)
    πŸ‘‰ Difference: Almost ZERO
    πŸ‘‰ Use: Readability > performance

  2. Many Fixed Values (10+ cases)
    πŸ‘‰ switch can be faster
    πŸ‘‰ Because engines may optimize it

  3. Complex Conditions (x > 10 && x < 20)
    πŸ‘‰ switch CANNOT handle this
    πŸ‘‰ Only if-else works

  4. Non-numeric / Sparse values
    πŸ‘‰ Optimization is less predictable
    πŸ‘‰ Performance β‰ˆ if-else

  5. Result (typical):
    πŸ‘‰ switch slightly faster only for large sets
    πŸ‘‰ Difference = microseconds

Use if-else when:
πŸ‘‰ Conditions involve ranges/logic
πŸ‘‰ Expressions are complex
πŸ‘‰ Fewer branches

Use switch when:
πŸ‘‰ Matching exact values
πŸ‘‰ Many cases (10+)
πŸ‘‰ Cleaner structure needed

If performance really matters, don’t use either: Use Object Lookup (Best Approach)

const map = {
  1: "One",
  2: "Two",
  3: "Three"
};

console.log(map[x]);

βœ” Time complexity: O(1)
βœ” Cleaner
βœ” Scalable