Skip to content

Latest commit

 

History

History
59 lines (49 loc) · 942 Bytes

File metadata and controls

59 lines (49 loc) · 942 Bytes

Switch



Guides



switch("Banana") {
  case "Banana":
    alert("Hello")
    break;
  case "Apple":
    alert("Welcome")
    break;
  default:
    alert("Neither");
}



Use unique scope for each case

// This will not work because result is already definied. (Uncaught SyntaxError: Identifier 'result' has already been declared)
switch("Banana") {
  case "Banana":
    const result = true
    console.log("Banana");
    break;
  case "Apple":
    const result = true
    console.log("Apple");
    break;
  default:
    alert("Neither");
}



// This will will work because each case got now his own scope.
switch("Banana") {
  case "Banana": {
    const result = true
    console.log("Banana");
    break;
  }
  case "Apple": {
    const result = true
    console.log("Apple");
    break;
  }
  default:
    alert("Neither");
}