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?
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?
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 namea
will refer to the variable declaration in the outer scope and you'll get 10.