-1

Please let me know the difference between the below two codes.

var a = 1; 
function b() { 
  a = 10; 
  return; 
} 
b(); 
console.log(a);

This prints 10 in console.

whereas the below code prints 1 in console

var a = 1; 
function b() { 
  var a = 10; 
  return; 
  function a() {} 
} 
b(); 
console.log(a); 

Thanks for help.

Daniel Schmidt
  • 11,605
  • 5
  • 38
  • 70
srikanth_k
  • 2,807
  • 3
  • 16
  • 18
  • 1
    Possible duplicate of [What is the scope of variables in JavaScript?](http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Filburt May 27 '16 at 06:51
  • In your first example, you are using the already declared a in function b which has a global scope, while in second example, you redeclare a in function b, which is limited to the scope of b. – Deepanshu Goyal May 27 '16 at 06:51

5 Answers5

2

In the first code there is one global variable 'a' which can be modified by any function and that change would be permanent.

But in the second code, there are two 'a' variable. Lets call them ag (a global) and al (a local). In second code b() function is modifying al (local variable a) not the global variable. But we are printing global variable in console.

Thats why results are varying in both the codes.

0

It's a metter of scope. In the first a is declared only out of function b, so when you edit a inside function b you are referencing the "outer" a.

In the second snippet you are re-declaring var a inside the function, so when you edit a variable inside the b function you are referencing the latest leaving untouched the "outer" a variable.

Hooch
  • 487
  • 3
  • 11
0

You have declared a in your second code inside the function hence the scope(availability) of your variable a persists only till function execution.

Once we return back from the function, local variable a is no more hence outer global a's value is printed.

riteshtch
  • 8,629
  • 4
  • 25
  • 38
0

The first example is quite straight-forward: You declare an a variable which your b function closes over. So b sets a to 10.

Your second example is deliberately complex and confusing. You declare an a variable that b closes over, but then shadow it with an a variable inside b. To make matters worse, there's also a function declaration of a that is also in scope in b. The var wins over the function declaration, because function declarations are processed before var statements are. The outer a is completely unaffected by b, so the console.log at the end logs 1.

That's easier to describe with a diagram of sorts:

var a = 1;            // <== The declaration `b` closes over
function b() {
  var a = 10;         // <== The inner `a` variable
  return;             // <== Returns from `b`
  function a() {}     // <=== Declaration that was processed immediately
                      // upon entering `b` (the `return` doesn't affect it
                      // at all), but which is then superceded by `var a`
} 
b(); 
console.log(a);       // Logs 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

when you are using var ,its scope will be local to its function. and without var it will be a global function.