JavaScript Switch Statements
Master switch statements for handling multiple conditions efficiently and learn modern switch patterns
35 min•By Priygop Team•Last updated: Feb 2026
Switch Statement Fundamentals
Switch statements provide an alternative to if...else chains when you need to compare a single value against multiple possible values. They are more readable and often more efficient than long if...else chains.
Basic Switch Examples
Example
// Basic switch statement
let day = 'Monday';
switch (day) {
case 'Monday':
console.log('Start of the work week');
break;
case 'Tuesday':
console.log('Second day of the week');
break;
case 'Wednesday':
console.log('Middle of the week');
break;
case 'Thursday':
console.log('Almost Friday');
break;
case 'Friday':
console.log('TGIF!');
break;
case 'Saturday':
case 'Sunday':
console.log('Weekend!');
break;
default:
console.log('Invalid day');
}
// Switch with numbers
let grade = 85;
switch (true) {
case grade >= 90:
console.log('Grade: A');
break;
case grade >= 80:
console.log('Grade: B');
break;
case grade >= 70:
console.log('Grade: C');
break;
case grade >= 60:
console.log('Grade: D');
break;
default:
console.log('Grade: F');
}
// Switch with expressions
let userType = 'premium';
let discount = 0;
switch (userType) {
case 'admin':
discount = 100;
break;
case 'premium':
discount = 25;
break;
case 'regular':
discount = 10;
break;
case 'guest':
discount = 5;
break;
default:
discount = 0;
}
console.log(`Discount: ${discount}%`);
// Switch without break (fall-through)
let month = 2;
let daysInMonth;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysInMonth = 31;
break;
case 4:
case 6:
case 9:
case 11:
daysInMonth = 30;
break;
case 2:
daysInMonth = 28; // Simplified, ignoring leap years
break;
default:
daysInMonth = 0;
}
console.log(`Days in month ${month}: ${daysInMonth}`);Modern Switch Patterns
Example
// Modern switch patterns (2025)
// Switch with object mapping
const actionHandlers = {
'create': (data) => console.log('Creating:', data),
'read': (id) => console.log('Reading:', id),
'update': (id, data) => console.log('Updating:', id, data),
'delete': (id) => console.log('Deleting:', id)
};
function handleAction(action, ...args) {
const handler = actionHandlers[action];
if (handler) {
handler(...args);
} else {
console.log('Unknown action:', action);
}
}
handleAction('create', { name: 'John' });
handleAction('read', 123);
handleAction('update', 123, { name: 'Jane' });
handleAction('delete', 123);
// Switch with function calls
function getGreeting(time) {
switch (true) {
case time < 12:
return 'Good morning';
case time < 17:
return 'Good afternoon';
case time < 21:
return 'Good evening';
default:
return 'Good night';
}
}
const currentHour = new Date().getHours();
console.log(getGreeting(currentHour));
// Switch with complex conditions
function getShippingCost(weight, destination) {
let baseCost = 0;
// Base cost by weight
switch (true) {
case weight <= 1:
baseCost = 5;
break;
case weight <= 5:
baseCost = 10;
break;
case weight <= 10:
baseCost = 15;
break;
default:
baseCost = 20;
}
// Multiplier by destination
let multiplier = 1;
switch (destination.toLowerCase()) {
case 'local':
multiplier = 1;
break;
case 'domestic':
multiplier = 1.5;
break;
case 'international':
multiplier = 3;
break;
default:
multiplier = 2;
}
return baseCost * multiplier;
}
console.log('Local shipping (2kg):', getShippingCost(2, 'local'));
console.log('International shipping (8kg):', getShippingCost(8, 'international'));Practice Exercise: Switch Statements
Example
// Exercise: Build a Calculator
function calculate(operation, a, b) {
switch (operation) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b === 0) {
throw new Error('Division by zero');
}
return a / b;
case '**':
return a ** b;
case '%':
return a % b;
default:
throw new Error('Unknown operation');
}
}
// Exercise: Build a Color Theme System
function getTheme(themeName) {
switch (themeName.toLowerCase()) {
case 'light':
return {
background: '#ffffff',
text: '#000000',
primary: '#007bff',
secondary: '#6c757d'
};
case 'dark':
return {
background: '#121212',
text: '#ffffff',
primary: '#bb86fc',
secondary: '#03dac6'
};
case 'blue':
return {
background: '#e3f2fd',
text: '#1565c0',
primary: '#1976d2',
secondary: '#42a5f5'
};
case 'green':
return {
background: '#e8f5e8',
text: '#2e7d32',
primary: '#388e3c',
secondary: '#66bb6a'
};
default:
return {
background: '#ffffff',
text: '#000000',
primary: '#007bff',
secondary: '#6c757d'
};
}
}
// Test the functions
console.log(calculate('+', 10, 5)); // 15
console.log(calculate('*', 4, 3)); // 12
console.log(calculate('/', 15, 3)); // 5
const lightTheme = getTheme('light');
const darkTheme = getTheme('dark');
console.log('Light theme:', lightTheme);
console.log('Dark theme:', darkTheme);