The parameters from the inner function shadows the outer function parameters and the variable. You can just access the variables inside the inner function, because when it tries to find a variable, it goes from its scope, if not found goes to the outer scope, in this case the outer function's scope and finds them.
But if you declare parameters in the inner function, they are founded and because you does not pass a value for them, their values are undefined. So you get undefined.
Or remove them from the inner function
return function() {
console.log("I am "+ name + " \nI am "+ age + " \nfrom "+address);
};
or just remove the address and call the inner function and pass to it the parameters.
return function(name, age) {
console.log("I am "+ name + " \nI am "+ age + " \nfrom "+address);
};
...
sayIntro()("john",27);
Example
function sayIntro(name,age){
var address = "TX";
return function(){
console.log("I am "+ name + " \nI am "+ age + " \nfrom "+address);
};
}
sayIntro("john",27)();
sayIntroare still in scope within the anonymous function – that’s not the problem. The problem is that there’s a whole new set of parameters with the same names that aren’t being passed values, and because they have the same names they hide the other variables.return function () {to fix.