0

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 ?

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
  • Braces are not required. Without knowing the purpose of that example code it's really hard to comment beyond that; the code makes little sense and if anything it looks like an example of things to *not* do. – Pointy Aug 04 '19 at 13:54

1 Answers1

2

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.

Jonathan Gray
  • 2,509
  • 15
  • 20