re: A New Coding Style for Switch Statements in JavaScript/TypeScript VIEW POST


This works when each case is discreet (having its own scope), ie each case terminates the switch with a break. But one of the features of switches is that things "fall through" each case until they hit a break. I've always liked that aspect of switches, even though it's often overlooked and can make it more confusing to read/grasp.

Just curious if setting each case in a scope like your suggested style specifically prevents that fall-through or breaks (renders unexpected behavior) code between switches due to the scope not carrying over.


Oh man, I love when I do a little further research and find out that the thing I'm talking about has a huge debate associated with it.

So it may be the case that your suggested syntax adds extra reinforcement for fall-through to be avoided as an intentional design practice.


You beat me to my own argument 😉

That said, I myself have also used fall-through statements on rare occasions.

That that said (is that a thing?), scoping each case using {} doesn't prevent you from falling through case statements anyways.

code of conduct - report abuse