Nested switch statement in javascript

前端 未结 4 1151
礼貌的吻别
礼貌的吻别 2021-02-05 05:34

Is it possible to use nested switch statement in javascript.

My code is some what look like

switch(id1)
{
case 1:
     switch(id2){
     ca         


        
4条回答
  •  囚心锁ツ
    2021-02-05 06:18

    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;`
    

提交回复
热议问题