Here is a code snippet I found in YDKJS.
function foo(){
var a = 2;
function baz(){console.log(a);}
bar(baz);
}
function bar(fn){
fn(); //closure happened
}
Can I say that because function baz got executed outside of its own scope, therefore closure happened?
I used to think that closure means in js , the inner functions have access of outer functions variables. But that is wrong. That’s what called lexical scoping.
If let’s say that during an interview I got asked: “could you tell me about closure in js and how it works?” can I show them this example snippet?