Tutorial :javascript consolidating switch statement



Question:

I have the following:

switch(state) {    case MyStates.StateA:       //do something       break;    case MyStates.StateB:       //do something       break;    case MyStates.StateC:       //do something       break;  

}

Say StateB and StateC both need to process the same bit of code, how can I avoid the duplication? Note that I cannot set MyStates.StateB = MyStates.StateC; they have to differ and can also be of different types. I want to do something like the following illegal statement:

case MyStates.StateB || case MyStates.StateC :  


Solution:1

switch(state) {    case MyStates.StateA:       //do something for A       break;    case MyStates.StateB:    case MyStates.StateC:       //do something for B and C       break;  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »