What is JavaScript Closure?

Whenever the inner function (a nested function) have access to variables defined outside of the inner function;s scope.

function SayHello(name) {
    // 'text' is local variable to 'SayHello'
    var text = 'Hello ' + name;

    // 'SayAlert' function has access to 'text' variable.
    var SayAlert = function() { alert(text); }

    return SayAlert;
}
Advertisements