Reputation: 11
function Outer(){
var a=10;
function Inner(){
var a = 20;
console.log(a);
}
Inner();
}
Outer();
In this codek I want the inner function to print the value of outer function's a(i.e 10). How do I achieve this?
Upvotes: 1
Views: 43
Reputation: 1261
When you delcare var a = 20;
on the fourth line above, you're redeclaring a variable that is already in scope and assigning it a new value. So the new value is what you get. If you remove that declaration, the name a
will refer to the variable declaration in the outer scope and you'll get 10.
Upvotes: 1