Alcance del bloque de caja del interruptor
let number = 2;
switch (number) {
case 1:
let message = "first number";
console.log(message)
break;
case 2:
let message = "second number";
console.log(message)
break;
case 3:
let message = "third number";
console.log(message)
break;
default
let message = "second number";
console.log(message)
break;
}
//This throws a syntax error: identifier "message"
//has already been declared
//Here is a way to fix this issue:
let number = 2;
switch (number) {
case 1: { // braces make the case statement a block
let message = "number" + number; // this remains in this block
console.log(message)
break;
}
case 2: {
let message = "number" + number; // this is a valid syntax
console.log(message)
break;
}
case 3: {
let message = "number" + number;
console.log(message)
break;
}
default
let message = "number" + number;
console.log(message)
break;
}
kid amogus