Functions

Closures in JavaScript

Closures are inner functions declared within outer functions.


Syntax
function outerFunction(passedParameters) {
    //closure
    function(passedParams2) {
        //closure code
    }
}

Notes

Closures have access to variables declared in the outer function, as well as global variables.


Example
function square(number) {
    function multiply(a, b) {
        return a * b;
    }
    return multiply(number, number);
}

See Also
Documentation

Closures - JavaScript | MDN

Add to Slack

< Anonymous Functions   |   Function Assignment >

© 2018 SyntaxDB. All Rights Reserved.