Thanks for contributing an answer to Stack Overflow! In most use cases, we have a variable or non-constant expression in the switch, and then match it. Many coders, reading that will not know what to make of it.For my own code, I'm more willing to use obscure structures from time to time, but if anyone else will look at it, I try to use clearer constructs. Type matters. Switching Details. The Overflow Blog Answer: Yes, in the JS Switch case you can use 2 or multiple values in one case. In the example, you can try with different values (1, 2, 3) and the result will be an alert box with a message. your coworkers to find and share information. It's quicker to added another case than it is to add another else if block (or at least it feels that way when trying to get a hotfix out to production)you are not answering the question. @xtempore I benefited from this answer and used in my ugly code just to get things working. By using our site, you acknowledge that you have read and understand our
I can't afford to take it personally: I am not the code, and the code is not me or my baby. Here the execution of case 3 starts from the line (*) and goes through case 5, because there’s no break.

@Alejandro Martin : Switch statements are useful because they provide code that is more concise and more readable. @collapsar case statements in JavaScript are fundamentally order-dependent. Anything beyond 5 I find switches are easier to read.

I think it is important for everyone to remember that the code is what we are talking about, not the people. here the case of 8 will now not match anything when I just edit the case that used to match 8.If you don't like the succession of cases, simply go for I was curious about the overhead of using a switch instead of the simpler if...else..., so I put together a jsFiddle to examine it... Where developers & technologists share private knowledge with coworkersProgramming & related technical career opportunitiesTo be safe, you should include a radix argument when using parseInt(). The closest you can get is:For cases like this it's usually better just to use a If you are trying to do something fast, efficient and readable, use a standard if...then...else structure like this:If you want to obfuscate it and make it awful (but small), try this:BTW, the above code is a JavaScript if...then...else shorthand statement. @Superman you make an excellent point. Free 30 Day Trial 2) How likely am I to go back and add additional cases? It is a great example of how NOT to write code unless obfuscation or code minification is the goal. I'd say it is not less clear as the structure is close to identical to the switch option. Q: Does Javascript switch case can have multiple cases or 2 values? Whether or not it is worth to be optimized, only profiling or careful contextual analysis can tell, and in case it is, you usually gain more by ordering the cases by decreasing frequency of runtime occurrence.
The Overflow Blog Podcast 248: You can’t pay taxes if the website won’t load But the truth is this that guys like me came here to get there things fixed and remember every beginner reached at top if they keep rolling and do not stop at the point whether it is better or worse. Just because you were able to create obscure sub-optimal code using a switch statement does not provide a rationale to remove switch statements from all languages. pada tutorial sebelumnya kita telah membahas tentang cara menggunakan switch case pada php.