DEV Community

Discussion on: Inline switch statements in javascript?

Collapse
 
besworks profile image
Besworks • Edited

You could accomplish this by wrapping your switch statement in an IIFE.

const result = (()=>{
  switch (key) {
    case 'foo' : return 4;
    default : return 1;
  }
})();
Enter fullscreen mode Exit fullscreen mode
Collapse
 
balastrong profile image
Leonardo Montini

Sure, wrapping the switch inside a function is a good way and IIFE looks like a really smart and concise solution, thanks for sharing! :)