DEV Community

Taufik Nurrohman
Taufik Nurrohman

Posted on

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

Top comments (0)