re: I've never become overly convinced that switch statements are that much cleaner than `if else if else if else if else` VIEW POST


A while back in college, I read somewhere that when there are a similar number of conditions in an if-else block and a switch statement, the switch statement is faster.

Recently, during a FreeCodeCamp JavaScript section project, I had to use a bunch of conditions (I know it could have been done in a better way) in multiple functions. I decided to try using switch statements since that would apparently be faster. But as soon as I wrote the first function, I realized that although it would be faster, it looked ugly. I used good old if-else blocks in the other functions.

And although I had to think quite a bit about the order of the conditions, it looked a bit cleaner than the former.

For me, I would use if-else blocks instead of switch statements until it's absolutely necessary. This was btw my first use of switch statements in around 2 years since last I used them in college.

code of conduct - report abuse