A closure is when an inner function remembers and can access variables from its outer function, even after the outer function has finished executing.
function outer() {
let name = "Sajin Joe";
function inner() {
console.log(name);
}
return inner;
}
let result = outer();
result();
Output :
Sajin Joe
Top comments (1)
Closures are formed EVERY time ANY function is formed - nested or otherwise. Every function retains access to the variables in its outer scope.
Misconceptions About Closures