title: JavaScript: Initializing variable in function
body: <p>A somewhat basic question here. I can see what is going on but I can't really understand why it would work this way.</p>
  
  <pre><code>a = false;
  var k = function() {
      console.log(a);
      var a = true
      console.log(a);
  }();
  </code></pre>
  
  <p>I would expect the log to read "false, then true" but "a" undefined at first. Can someone please elaborate why it does this. </p>
  
  <p>Ps. I'm not looking for an answer telling me what I should do, I'm looking for an explanation of the inner workings of this piece of JavaScript. </p>
  
  <p>Thanks in advance</p>