javascript does not have a block scope, so declaring variables in a switch block does not work as you expected.
In addition, due to the hoisting variable , all variable declarations in the function block are raised to the top by the interpreter, and your code will look like this:
function test1() { var test; var test; switch(type) { case 1: test = "Hello One" break; case 2: test = "Hello Two" break; } }
After the lift is completed, it is easy to see why the first block is incorrect.
source share