Alternative to JavaScript’s switch statement with a functional twist
Hajime Yamasaki Vukelic
1.2K13

The problem of switch not being an expression has _always_ bugged me.

Some time ago I found this solution too: https://hackernoon.com/rethinking-javascript-eliminate-the-switch-statement-for-better-code-5c81c044716d

As a comparison, I’d say your solution has clearer syntax. The other solution has an interesting point free style and better performance (though I’ve yet to see a case where performance matters in switch cases).

Both can also be easily strongly typed, like in Typescript. In both cases the error given would be about mismatched types, which could be pretty puzzling though.

Nice work!

Like what you read? Give RedGlow a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.