Javascript Control Flow

JavaScript switch Statement


Javascript swtich statment is used to make decison making. If you have multiple condition and one of must have to execute or all of them may wrong then swith statement is used. Expression:

switch(variable/expression) {
    case value1:  
        // body of case 1
        break;

    case value2:  
        // body of case 2
        break;

    case valueN:
        // body of case N
        break;

    default:
        // body of default
}

Example:

const expr = 'Mango';
switch (expr) {
  case 'oranges': 
    console.log('Oranges are orange');
    break;
  case 'Mango':
  case 'papaya':
    console.log('Mango and papya are fruits.');
    // Expected output: "Mango and papya are fruits."
    break;
  default:
    console.log("No condition falls");
}