Skip to content

Simplify your switch statements

Posted on:December 31, 2020 at 03:22 PM

The switch statement is a powerful tool for every developer. The MDN documentation describes it as follows:

The switch statement evaluates an expression, matching the expression’s value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.

-MDN

Let’s jump into an example. We need to write a function which converts a month number to the month name.

Using a switch statement, we could write something like this:

function getMonthName(monthNumber) {
  switch (monthNumber) {
    case 1:
      return "January";
    case 2:
      return "February";
    case 3:
      return "March";
    // ...
    case 10:
      return "October";
    case 11:
      return "November";
    case 12:
      return "December";
  }
}

const monthName = getMonthName(10);
console.log(monthName); // "October"

This is perfectly fine but lots of the code required for this mapping is just boilerplate. Let’s replace the switch statement with an object literal:

function getMonthName(monthNumber) {
  const months = {
    1: "January",
    2: "February",
    3: "March",
    // ...
    10: "October",
    11: "November",
    12: "December",
  };
  return months[monthNumber];
}

const monthName = getMonthName(10);
console.log(monthName); // "October"

The outcome is exactly the same but using a object literal makes our code more concise (30% shorter) and it also improves the code readability.