I am trying to write a switch statement, but I have a strictly defined case, and I want to use as little code as possible. Since I was wondering how to do this, it occurred to me whether it would be possible to add an if statement to a switch, so if this if statement is true, to add more cases to my switch. For instance:
switch(myVar) { case 1: return 'Your variable is 1'; case 2: return 'Your variable is 2'; if(yourVar && yourVar === true) { case 3: return 'Your variable is 3'; } default: return 0; }
PS The real scenario I'm using is more complicated and the code is really long, so any greetings are welcome if they are suitable for use.
source share