Replace Switch Statements with Object Maps for Cleaner JavaScript
Learn how modern JavaScript lets you replace verbose if‑else and switch statements with concise object literal mappings and the powerful ES6 Map structure, improving readability, reducing errors, and simplifying maintenance for both simple and complex branching logic.
Problems with traditional switch statements
First, look at the typical usage of a switch statement:
This approach has several issues:
Verbose and repetitive – each case requires writing the case keyword and a return or break statement.
Easy to make mistakes – forgetting a break leads to unintended fall‑through behavior.
Hard to maintain – adding or modifying branches requires careful insertion into large blocks of code.
Poor readability – especially when each case contains substantial logic.
Using object literal mapping instead of switch
Object mapping is the most direct way to replace a simple switch statement:
Handling complex logic
If each branch contains more complex logic, you can store functions as the object’s values:
Using the Map data structure
ES6 introduced the Map data structure, which offers stronger capabilities than plain object literals, especially for these scenarios:
Keys are not limited to strings; they can be any type, including objects and functions.
Frequent addition/removal of key‑value pairs.
Preserving insertion order.
Directly obtaining the size of the mapping.
Usage example
Function mapping and chaining
Map is well suited for implementing command or strategy patterns:
class Calculator {
constructor() {
this.operations = new Map([
['+', (a, b) => a + b],
['-', (a, b) => a - b],
['*', (a, b) => a * b],
['/', (a, b) => a / b],
['%', (a, b) => a % b],
['', (a, b) => a b]
]);
}
calculate(a, operator, b) {
const operation = this.operations.get(operator);
if (!operation) {
throw new Error(`Unsupported operator: ${operator}`);
}
return operation(a, b);
}
addOperation(operator, fn) {
this.operations.set(operator, fn);
return this; // support chaining
}
}
const calc = new Calculator()
.addOperation('log', (a, b) => Math.log(a) / Math.log(b));
console.log(calc.calculate(10, '+', 5)); // 15
console.log(calc.calculate(10, 'log', 10)); // 1Modern JavaScript provides multiple tools to improve code quality; embracing object mappings and Maps lets you write cleaner, more maintainable code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaScript
Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
