Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

JavaScript Closures

// javascript closure example

// outer function
function greet() {
    // variable defined outside the inner function
    let name = 'John';
    // inner function
    function displayName() {

        // accessing name variable
        return 'Hi' + ' ' + name;
      
    }
    return displayName;
}

const g1 = greet();
console.log(g1); // returns the function definition
console.log(g1()); // returns the value
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #JavaScript #Closures
ADD COMMENT
Topic
Name
7+2 =