JavaScript's Switch Statement

Alternatives to the JavaScript Switch Statement

If you're not a fan of JavaScript's switch statement, you're not alone. It's clunky, and it easily becomes unreliable as a program's complexity increases. It can make following logic difficult, especially for anyone who didn't write the original code. And in some cases, it can seem downright uncooperative, slowing the programming process to a crawl as you write conditional statements around it just to get it to perform as desired.

Although some might say, "Switch statements aren't that bad," it's safe to say there aren't many who would give them a glowing recommendation. Most programmers use them only because there's no better alternative.

But maybe there is.

In this article, Orlando La examines both the pitfalls in switch-statement use and a couple of solutions that have the potential to make your life a little easier.