JavaScript Notes

Switch

The switch statement evaluates an expression, matching the expression's value against a series of case clauses, and executes statements after the first case clause with a matching value, until a break statement is encountered. If it has not break statement than it execute and the behaviour is called "fall-through". The default clause of a switch statement will be jumped to if no case matches the expression's value.

switch (expression) {
  case caseExpression1:
    statements
  case caseExpression2:
    statements
  // …
  case caseExpressionN:
    statements
  default:
    statements
}
  • expression(optional) - An expression is whose result is matched against each case.

  • case caseExpressionN(optional) - number cases matches with the expression if match than execute or exit from the block.

  • default(optional)

    If any of the above case caseExpressionN is not match to the 'expression', than is comes to default and execute the statement inside it.

    If no default clause is found, the program continues execution at the statement following the end of switch. multiple default clauses will result in a Syntax Error.

  • break You can use the break statement within a switch statement's body to break out early, often when all statements between two case clauses have been executed. Execution will continue at the first statement following switch.

      const foo = 0;
      switch (foo) {
        case -1:
          console.log("negative 1");
          break;
        case 0: // Value of foo matches this criteria; execution starts from here
          console.log(0);
        // Forgotten break! Execution falls through
        case 1: // no break statement in 'case 0:' so this case will run as well
          console.log(1);
          break; // Break encountered; will not continue into 'case 2:'
        case 2:
          console.log(2);
          break;
        default:
          console.log("default");
      }
      // Logs 0 and 1
    

    In the appropriate context, other control-flow statements also have the effect of breaking out of the switch statement. For example, if the switch statement is contained in a function, then a return statement terminates the execution of the function body and therefore the switch statement. If the switch statement is contained in a loop, then a continue statement stops the switch statement and jumps to the next iteration of the loop.

  • continue

    The continue statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.

  • Lexical scoping

case "say_hello":
    const message = "hello";
    console.log(message);
    break;
  case "say_hi
  case "....
....
//its gives an error
//so to this fix this 
  case "say_hello": {
    const message = "hello";
    console.log(message);
    break;
  }
  case "....
  • whenever you need to use let or const declarations in a case clause, wrap it in a block.

Taking advantage of fall-through

example 1

const Animal = "Giraffe";
switch (Animal) {
  case "Cow":
  case "Giraffe":
  case "Dog":
  case "Pig":
    console.log("This animal is not extinct.");
    break;
  case "Dinosaur":
  default:
    console.log("This animal is extinct.");
}
//output 
//This animal is not extinct.

Due to fall-through multiple case has same statement work.

example 2:

const foo = 1;
let output = "Output:";
switch (foo) {
  case 0:
    output += "So ";
  case 1: //its start from here 
    output += "What "; //add what to Output:
    output += "Is ";// add is
  case 2:
    output += "Your ";//add your
  case 3:
    output += "Name";//name so on..
  case 4:
    output += "?";
    console.log(output); //Output:what is your name ?
    break;
  case 5:
    output += "!";
    console.log(output);
    break;
  default:
    console.log("Please pick a number from 0 to 5!");
}
//Output:what Is Your Name ?

Did you find this article valuable?

Support Tanmaya Tatsat Mahapatra by becoming a sponsor. Any amount is appreciated!