switch statement and scopes in ES2015
<p>Consider this ES2015 module and the behavior when run in node v4.4.5.</p>

<pre><code>'use strict'

const outer = 1

switch ('foo') {
  case 'bar':
    const heyBar = 'HEY_BAR'
    break
  case 'baz':
    const heyBaz = 'HEY_BAZ'
    break
  default:
    const heyDefault = 'HEY_DEFAULT'
}
console.log(
  outer, // 1, makes sense, same top-level scope
  heyBar, // undefined. huh? I thought switch did NOT create a child scope
  heyBaz, // undefined. huh? I thought switch did NOT create a child scope
  heyDefault) // 'HEY_DEFAULT' makes sense
</code></pre>

<p>This seems internally inconsistent to me. If the switch statement did NOT create a lexical scope, I would expect all the <code>hey*</code> variables to be part of the main scope and to all behave consistently. If the switch statement did create a lexical scope, I would still expect them to be consistent, but the variables declared in the <code>case</code> clauses behave like they are in a child scope, whereas the variables in the <code>default</code> clause behaves like it is in the outer scope.</p>

<p><strong>My question is</strong> are there any child scopes involved in a switch statement, and if so what are the details of how they behave?</p>

<p>In node v6.4.0, the behavior is different. It looks like the switch block does indeed create a child block scope.</p>

<pre><code>ReferenceError: heyBar is not defined
</code></pre>

<p>And that seems a lot more straightforward to understand.</p>