Skip to content
Concept 19 // Module 02: Values, Operators & Control Flow

Conditional Statements: if, else & switch

Module 02: Values, Operators & Control Flow•Concept 19 of 100• 5 min read

Conditional Statements: if, else & switch

Conditional statements branch execution paths based on truthiness. `if...else` provides hierarchical logical evaluation, while `switch` offers structured multi-way value matching optimized via engine jump tables.

The Core Concept

Syntax Reference
if (condition1) {
  // branch 1
} else if (condition2) {
  // branch 2
} else {
  // default branch
}

switch (expression) {
  case value1:
    break;
  default:
}

Live Interactive Studio

Evaluates branch conditions top-to-bottom and executes the first matching block.
JavaScript Editor
Loading...
Console Output
Waiting for output...

Key Code Patterns in Action

01

Multi-way Branching with if...else

const score = 88;
let grade;

if (score >= 90) {
  grade = 'A';
} else if (score >= 80) {
  grade = 'B';
} else {
  grade = 'C';
}

console.log("Final Grade:", grade); // Final Grade: B
Evaluates branch conditions top-to-bottom and executes the first matching block.
02

Clean Pattern Matching with switch

const role = 'editor';

switch (role) {
  case 'admin':
    console.log("Full Access");
    break;
  case 'editor':
    console.log("Edit Access"); // Matches here!
    break;
  default:
    console.log("Read Only");
}
Switch statements use strict equality (===) for case comparisons and require `break` to prevent unintentional fallthrough.
03

1. Core Usage: Conditional Statements

// Core syntax and usage for Conditional Statements: if, else & switch
const feature = 'conditional-statements-if-else-switch';
console.log('Concept 19:', feature);
console.log('Active:', true);
Illustrates the fundamental invocation and syntax structure for Conditional Statements: if, else & switch.
04

2. Defensive Handling & Edge Case

// Edge-case defensive handling
function handleSafe(input) {
  if (input === null || input === undefined) return 'safe-fallback';
  return String(input).trim();
}
console.log(handleSafe('conditional-statements-if-else-switch'));
console.log(handleSafe(null));
Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with Conditional Statements: if, else & switch.
05

3. Clean Architecture & Production Pattern

// Production-grade pattern for Conditional Statements: if, else & switch
const settings = Object.freeze({
  id: 'conditional-statements-if-else-switch',
  ready: true,
  timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);
Demonstrates clean, immutable production code avoiding unintended state mutations.

Real-World Engineering Takeaways

Permission Checks: Validating roles and user access.
State Machines: Dispatching actions based on enum status.
Form Validation: Checking required input constraints.

Under the Hood: Engine & Memory Mechanics

How JavaScript Executes This
Engines optimize dense switch blocks into branch tables (jump tables), executing case lookups in O(1) time without evaluating each comparison sequentially.

Common Pitfalls & Traps

Always include break statements in switch cases unless fallthrough is intentional

switch(x) { case 1: doThing(); case 2: doOther(); } // Fallthrough executes both!

Missing break statements cause execution to cascade through subsequent case blocks regardless of condition.

Quick Knowledge Check

Test your understanding on these 10 questions (one by one):

Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model

What is the core purpose of Conditional Statements: if, else & switch in JavaScript?

// Concept 19: Conditional Statements: if, else & switch
// Understanding the core mental model
1 / 10