Question | Answer |
---|---|
What is closure in JavaScript? | Answer |
Explain event delegation in JavaScript. | Answer |
What are the differences between let , var , and const ? |
Answer |
Closure is a fundamental concept in JavaScript where a function retains access to its lexical scope, even after the function has finished executing. It means the function 'remembers' its surrounding scope's variables and parameters, even if it's called outside of that scope.
function outerFunction() {
let outerVariable = 'I am from the outer function';
return function innerFunction() {
console.log(outerVariable);
}
}
let newFunction = outerFunction();
newFunction(); // Output: I am from the outer function