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

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

Some time ago I found this solution too:

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.