0

I was reading a tutorial on Javascript basics & how it is different from other languages like C, C++ in terms of having function-level scope rather than block-level scope, but, came across this script which got me confused!

So, Basically:

  1. I want to know how does outcome of the following code comes to be '1'?
  2. And exactly what is the role of function a() {} ? I mean its never called and in function b() {} also there's a return statement before its declaration. But if i change the function name , the alert gives the value '10'.Why?

    <script>
        var a = 1;
    
        function b() {  
            a = 10;
            return;
            function a() {}
        }
    
        b();
        alert(a);
    </script>
    
Joren
  • 3,068
  • 25
  • 44
n4m31ess_c0d3r
  • 3,028
  • 5
  • 26
  • 35
  • @Sarath I have still one doubt remaining why the functional decalaration " function a() {} " is being parsed into functional expressions "var a= function a() {} " – n4m31ess_c0d3r Nov 14 '13 at 14:02
  • The difference is that `var a= function() {}` is defined at run-time, whereas `function a() {}` is defined at parse-time ., http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname – Sarath Nov 14 '13 at 14:07

0 Answers0