JavaScript : Switch Statement - 2

Part 2

Namaste, In this blog I will discuss more use cases of Switch statement.

Multiple Values in Switch..Case

When multiple values are given in a switch..case

  • There is no error

  • Only the last value in the case is matched

      let a=10;
      switch(a) {
        case 1,2,3:
          console.log("a:",a);
        case 8,9,10:
          console.log("a2:", a) // a2:10
      }
    

    In the above code , 'a' is matched with second case's value 10.

  • All values except the last value in the case are ignored

    In the above code values 8,9 are ignored i.e they are not matched.

// multiple values in switch..case

let a=10;
// for - 10 works
// for - 8,9 - default
switch(a) {
    case 1,2,3:
        console.log("a", a);
        break;
    case 8,9,10:
        console.log("a2",a);
        break;
    default:
        console.log("default -a", a);
        break;
}

In the above code

  • If the position of value 10 is changed then default clause is executed

  • The matching value has to be the last value in the given case

Multiple values :Solution

If one piece of code has to be executed use multiple fall through cases.

// solution
let a=10;
switch(a) {
    case 8:
    case 9:
    case 10:
        console.log("a", a);
        break;
    default:
        console.log("defualt :",a);
        break;            
}

In the above code if 'a' value is 8,9 or 10 the common set of instructions will be executed.

Default Case

The default case can be placed at any location within switch statement.

// default can appear any where

a=11;
switch(a) {
    default:
        console.log("default");
        break;
    case 7:
            console.log("7");
            break;
    case 8:
        console.log("8");
           break;
    /*default: // Error
        console.log("default2"); 
        break; */
    case 7: // No Error 
        console.log("7");
        break;
    case 9:
        console.log("9");
        break;
    case 10:
        console.log("10");
}

In the above code for switch..case

  • Cannot have more than 1 default

  • For case clause a given value can be duplicated e.g case 7 occurs 2 times but there is no error.

  • The first matching case is executed

Switch statement with Objects

Objects are compared by reference in switch statement

// with objects
let obj= {
    v:10
}
obj2 = obj;
let obj3 = {
    v:10
}
let obj4 = {
    v:10
}

switch(obj) {
  case {a:10} :
    console.log("a:10", obj);
    break;
  case obj3 :
    console.log("obj3");
    break;
  case obj4:
    console.log("obj4");
    break;
  case obj2:
    console.log("obj2"); // output
    break;
  default:
    console.log("default");
}

In the above code , case of obj2 is executed as the comparison is reference based.

Complete Code Listing

//Objective : Switch statement use cases
//Author : Mahavir


let a=10;
switch(a) {
    case 1,2:
        console.log("a", a);
        break;
    case 8,9,10:
        console.log("a2",a); // output - a2 - 10
        break;// 8,9 does not execute
    default:
        console.log("default -a", a);
        break;
}

//solution
switch(a) {
    case 8:
    case 9:
    case 8:
        console.log("a", a);
        break;
    default:
        console.log("defualt :",a);
        break;            
}

// default can be put any where in switch statement
a=11;
switch(a) {
    default:
        console.log("default");
        break;
    case 7:
            console.log("7");
            break;
    case 8:
        console.log("8");
           break;
    /*default: // Error
        console.log("default2"); 
        break; */
    case 7: // No Error
        console.log("7");
        break;
    case 9:
        console.log("9");
        break;
    case 10:
        console.log("10");
}


// with objects
let obj= {
    v:10
}
obj2 = obj;
let obj3 = {
    v:10
}
let obj4 = {
    v:10
}

switch(obj) {
  case {a:10} :
    console.log("a:10", obj);
    break;
  case obj3 :
    console.log("obj3");
    break;
  case obj4:
    console.log("obj4");
    break;
  case obj2:
    console.log("obj2"); // output - obj2
    break;
  default:
    console.log("default");
}

Did you find this article valuable?

Support Programming with Mahavir by becoming a sponsor. Any amount is appreciated!