I'm trying to write a "switch" statement but I have strictly defined case and I want to use as little code as I can. So as I was wondering how to do it one thought came to my mind, is it possible to add "if" statement in "switch" so if this "if" statement is true to add more cases to my "switch". For example:
switch(myVar) {
case 1: return 'Your variable is 1';
break;
case 2: return 'Your variable is 2';
break;
if(yourVar && yourVar === true) {
case 3: return 'Your variable is 3';
}
default: return 0;
}
P.S. The real scenario I am using is way more complicated and the code is really long so any sugestions are welcomed if they fit the use case.
Aucun commentaire:
Enregistrer un commentaire