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

Taufik Nurrohman @taufik_nurrohman

About: I have a dream, but it won’t make me rich.

Location:
Banyumas, Indonesia
Joined:
Jun 20, 2019

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

Publish Date: Dec 29 '19
8 0

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

Comments 0 total

    Add comment