title: javascript- Uncaught SyntaxError: Identifier * has already been declared
body: <pre><code>console.log(a) //output:ƒ a(){}
  var a = 1;
  function a(){};
  var a = 10;
  console.log(a) //output:10
  </code></pre>
  
  <p>====================</p>
  
  <pre><code>var a = 1;
  if(true){
    function a(){};
    var a = 10;
  }
  console.log(a) // this code throws Uncaught SyntaxError: Identifier 'a' has already been declared
  </code></pre>
  
  <p>both above code snippets are same except the if block.why does the latter throws error when its permissible in javascript to delcare same variable twice in the same scope with var as below</p>
  
  <pre><code> function a(){};
   var a = 10; //no error
  </code></pre>
  
  <p>Also for a slightly different scenario after removing var from `var a = 10 in the above code ,then it works fine but output is surprising   </p>
  
  <pre><code> var a = 1;
   if(true) {
     function a(){};
     a = 10;
   }
   console.log(a) //output:ƒ a(){}
  </code></pre>
  
  <p>I am surprised to see this output as I am expecting 10 ..because two variables declared inside the if block refer to the same variable declared above as javascript var doesnt respect block scope but functional scope...so why not the output for above should be 10?
  where as the below code outputs 10 as i expected when replaced the function definition with function expression.</p>
  
  <pre><code>  var a = 1;
    if(true) {
      var a = function(){ console.log() }
      a = 10;
    }
    console.log(a) //output:10
  </code></pre>