38

Is it possible to use nested switch statement in javascript.

My code is some what look like

switch(id1)
{
case 1:
     switch(id2){
     case 1:{
        switch(id3){
        case 1:{}
        case 2:{}
        }
    }
     case 2:{
        switch(id4){
        case 1:{}
        case 2:{}
        }
     }
}
case 2:
}

If yes then it is a good practice to do or we can use any alternate approach.

5
  • 4
    it makes no sense to switch with the same variable. Commented Jul 14, 2016 at 9:52
  • 1
    I can say that without any doubt, this is not clean code. That said, the best approach depends on what it's actually about. Commented Jul 14, 2016 at 9:52
  • 1
    @NinaScholz I assume that it was pseudocode. Commented Jul 14, 2016 at 9:52
  • 1
    It should be possible (don't forget break statements). Recommended -- maybe not. Commented Jul 14, 2016 at 9:53
  • Yes it is a pseudocode. I am planning to write code and as requirement I am prepare for code. Commented Jul 14, 2016 at 9:54

5 Answers 5

64

Your approach is absolutely fine.

You can make the switch nesting less complex by using switch (true):

switch (true) {
  case ((id1 === 1) && (id2 === 1) && (id3 === 1)) :
  case ((id1 === 1) && (id2 === 1) && (id3 === 2)) :
  case ((id1 === 1) && (id2 === 2) && (id3 === 1)) :
  case ((id1 === 1) && (id2 === 2) && (id3 === 2)) :
  case ((id1 === 2) && (id2 === 1) && (id3 === 1)) :
  case ((id1 === 2) && (id2 === 1) && (id3 === 2)) :
  case ((id1 === 2) && (id2 === 2) && (id3 === 1)) :
  case ((id1 === 2) && (id2 === 2) && (id3 === 2)) :
}
Sign up to request clarification or add additional context in comments.

2 Comments

you can omit the parenthesis in the case clauses.
Force of habit, Nina. :-)
13

Yes, you can use inner switch like this way,

Please check this demo : https://jsfiddle.net/1qsfropn/3/

var text;
var date = new Date()
switch (date.getDay()) {
 case 1:
 case 2:
 case 3:
 default:
    text = "Looking forward to the Weekend";
    break;
 case 4:
 case 5:
    text = "Soon it is Weekend";
    break;
 case 0:
 case 6:
      switch(date.getFullYear()){
      case 2015:
        text = "It is Weekend of last Year.";
      break;
      case 2016:
        text = "It is Weekend of this Year.";
      break;
      case 2017:
        text = "It is Weekend of next Year.";
      break;
      default:
      text = date.getDay();
      break;
    }
break;
}
document.getElementById("demo").innerHTML = text;`

Comments

8

You can use a nested switch statement but that can quickly become a spaghetti code and therefore it is not recommended. I would rather use functions with the nested switch statement for code clearance or maybe use recursive function depending on what the code is supposed to do.

This is only a pseudo-code but I hope it gives you some idea on how to implement it. You have to be carefull to make the recursion stop on some given value of the ID. This pseudo-code increments the value of the ID by 1 if the value of the ID is 1, and increments by 2 if the value is 2. If the value is not 1 or 2 the recursion ends.

function recursiveSwitch(var id) {
    switch(id) {
       case 1: 
           recursiveSwitch(id + 1);
           break;
       case 2
          recursiveSwitch(id + 2)
          break;
       default:
          return;
     }
}

Comments

0

Basically, it's possible but I think it depends on the complexity of the nesting if it's recommended to use nested switch statement or to use functions with the nested switch statement as Ómar Óskarsson has suggested.

2 Comments

This is not really an answer, you're just confirming Omar's answer
@Shogunivar No! Actually I say it depends on the complexity!
0

It is possible to use nested switch statements in JS. Buth they are generally not considered a best practice. They:

  • Increase complexity - Makes code harder to read and understand
  • Harder to test - Need more test cases to cover all nested branches
  • Violate SRP - The method is doing too much

The better approach is to extract each case into separate private methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.