how to execute the closures in javascript code example
Example 1: closure in js
A closure gives you access to an outer function’s scope from an inner function
//example
function init() {
var name = 'Mozilla'; // name is a local variable created by init
function displayName() { // displayName() is the inner function, a closure
alert(name); // use variable declared in the parent function
}
displayName();
}
init();
Example 2: closure in javascript
function OuterFunction() {
var outerVariable = 1;
function InnerFunction() {
alert(outerVariable);
}
InnerFunction();
}