title: JavaScript hoisting for multiple declarations of the same variable
body: <p>I was trying to understand JavaScript hoisting and from what I have understood, memory space is set aside for all the variable declarations before the execution of the code.</p>
  
  <p>I would like to understand how the interpreter works through hoisting in case of multiple declarations for the same variable. Consider the following code:</p>
  
  <pre><code> console.log(a);
  
   //first declaration
   function a() {
       console.log('hello');
   }
  
   //second declaration
   var a = 2;
  </code></pre>
  
  <p>The output is the function declaration:</p>
  
  <pre><code>function a() {
      console.log('hello');
  }
  </code></pre>
  
  <p>So, I am assuming the first declaration the interpreter encounters is the one which is stored in memory. However, changing the order of declarations to: </p>
  
  <pre><code>console.log(a);
  
  //first declaration
   var a = 2;
  
  //second declaration
  function a() {
      console.log('hello');
  }
  </code></pre>
  
  <p>results in the same output! The interpreter ignores the first declaration and stores the second declaration in memory. Why is this happening? Shouldn't the output in the second case be <code>undefined</code>?</p>