In C language, you cannot declare any variables inside 'case' statements.
switch ( i ){
case 1:
int a = 1; //error!
break;
}
However, you can when you use with curly parentheses.
switch ( i ){
case 1:
{// create another scope.
int a = 1; //this is OK.
}
break;
}
In Javascript case, can I use var directly inside case statements?
switch ( i ){
case 1:
var a = 1
break
}
It seems that there is no error but I'm not confident this is grammatically OK.
awill be local to the function, not to the switchvar, yes. you can see many examples in the linked postcases because there is only one underlying block.