How can I use ranges in a switch case statement using JavaScript?
问题 How can I use ranges in a switch case statement using JavaScript? So, instead of writing code for each and every single possibility, I\'d like to group them in ranges, For example: switch(myInterval){ case 0-2: //doStuffWithFirstRange(); break; case 3-6: //doStuffWithSecondRange(); break; case 6-7: //doStuffWithThirdRange(); break; default: //doStuffWithAllOthers(); } 回答1: You have at least four options: 1. List each case As shown by LightStyle, you can list each case explicitly: switch