Closure in Javascript
What is a Closure? In JavaScript, when you create a function inside another function, the inner function can use the variables of the outer function. Normally, once a function finishes running, its...

Source: DEV Community
What is a Closure? In JavaScript, when you create a function inside another function, the inner function can use the variables of the outer function. Normally, once a function finishes running, its variables are gone. But in closures, the inner function keeps those variables in memory. function outer() { let name = "John"; function inner() { console.log(name); } return inner; } const myFunction = outer(); myFunction(); Difference Between Function and Closure Function A normal function executes its code and then finishes. Once it is done, the variables inside the function are removed from memory and cannot be used again.In a normal function, variables are often global and can be changed by anyone, which makes them unsafe. let count = 0; function add() { count++; console.log(count); } Closure A closure is a function that remembers the variables from its outer function even after the outer function has finished running. These variables stay in memory and can be used later whenever the fun