title: How to understand function declaration in switch case block?
body: <p>Here is the code</p>
  <pre><code>switch (0) {
      case 0:
          console.log(&quot;Case 0 started&quot;);
          foo();
          console.log(&quot;Case 0 ended&quot;);
          break;
  
      case 1:
          function foo() { console.log(&quot;foo is a function&quot;) }
          break;
  }
  
  console.log(&quot;But here foo is:&quot;, foo);
  foo();
  </code></pre>
  <p>Here is the output of the code:</p>
  <p><a href="https://i.stack.imgur.com/Hc9Jx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Hc9Jx.png" alt="enter image description here" /></a></p>
  <p>Based on the output, it seems that function <code>foo</code> being declared even in a case which is impossible to be hit because <code>1 !== 0</code> forever. And <code>foo</code> is only available in the scope of the <code>switch case</code> block. It is not accessible from the top level as we can tell from line 13 of the code. <code>foo</code> is undefined.</p>
  <p>But if we make some change to the code by adding another declaration like this:</p>
  <pre><code>switch (0) {
      case 0:
          console.log(&quot;Case 0 started&quot;);
          // Add this declaration.
          function foo() { }
          foo();
          console.log(&quot;Case 0 ended&quot;);
          break;
  
      case 1:
          function foo() { console.log(&quot;foo is a function&quot;) }
          break;
  }
  
  console.log(&quot;But here foo is:&quot;, foo);
  foo();
  </code></pre>
  <p><code>foo</code> is available in the top level scope.</p>
  <p><a href="https://i.stack.imgur.com/JMgQx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JMgQx.png" alt="enter image description here" /></a></p>
  <p>How to understand this behave of the code? That would be very much appreciated with related specs provided. Thank you!</p>