I'm catching up on javascript that I have not used since some time, doing so by inspiring most of my coding style from the Modern Javascript coding guide by AirBnb
In the paragraph regarding switch
I think there is an inconsistency. But that's not as if the Airbnb guide had been read by tons of guy before me...
Check the line for case 4
which alone does not use curly braces, but rather no braces at all like in the style before which is considered bad. However the default
case comes back at using curly braces.
switch (foo) {
case 1: {
let x = 1;
break;
}
case 2: {
const y = 2;
break;
}
case 3: {
function f() {
// ...
}
break;
}
case 4:
bar();
break;
default: {
class C {}
}
}
If it is not an inconsistency, then what would be the sound reason for this different case 4
?
There is no inconsistency. The curly braces are used as a scope for the lexical declarations. Case 4 is the only case without any such declaration.