function foo() {
var bar = 'no'
setInterval(function() { console.log(bar); }, 1000);
}
When I execute this piece of code, I got the following output: no
, so the output is correct. But when I execute the next piece of code, when I pass the function bar
as a parameter to that anonymous function, I don't know exactly why the output is undefined
function foo() {
var bar = 'no'
setInterval(function(bar) { console.log(bar); }, 1000);
}
If I pass the variable as a parameter, why is undefined? If there was also a variable call bar
inside the anonymous function, I know that variable would be rewritten with the inner function value, but I can't understand this behavior