This Code Snippet Will Make You Think That `switch ... case` Statements Are Obsolete

taufik_nurrohman

Taufik Nurrohman

Posted on December 29, 2019

This Code Snippet Will Make You Think That `switch ... case` Statements Are Obsolete

Before

let b;

switch (a) {
  case '#':
    b = 'one';
    break;
  case '##':
    b = 'two';
    break;
  default:
    b = 'unknown';
}
Enter fullscreen mode Exit fullscreen mode

After

let b = ({
    '#': 'one',
    '##': 'two'
})[a] ?? 'unknown';
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
taufik_nurrohman
Taufik Nurrohman

Posted on December 29, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related