DEV Community

Raj
Raj

Posted on

closure

when learned about closure,it felt a little confusing .but once we understand

a closure happens when an inner function remembers and uses variable from its outer function even after the outer function has finished

example

function outer() {
let name = "raj";
function inner (){
console.log(name);
}
return inner ;
}
let result = outer();
result();

 //raj
Enter fullscreen mode Exit fullscreen mode

why can result () still access name?

because the inner() function remember the name variable from outer().that memory is called closure.

easy is remebered betwwen each fuction call.
colsure a function that remebers the variables from its outer scope

Top comments (0)