How we call closures in javascript code example
Example 1: what is closure in javascript
function OuterFunction() {
var outerVariable = 100;
function InnerFunction() {
alert(outerVariable);
}
return InnerFunction;
}
var innerFunc = OuterFunction();
Example 2: js closure examples
function outer() {
var counter = 0;
function incrementCounter() {
return counter++;
}
return incrementCounter;
}
const count = outer();
count();
count();
count();
Example 3: closure in javascript
function init() {
var name = 'Mozilla';
function displayName() {
alert(name);
}
displayName();
}
init();