JavaScript allows you to declare a function within another function, and the local variables still can be accessible even after returning from the function you called.
In other words, a closure is the local variables for a function which is kept alive after the function has returned.
An example :
function goodMorning(name) {
var text = ‘Good Morning ‘ + name; // local variable
var goodMorningAlert = function() { alert(text); }
return sayAlert;
}
var goodMorningAlert2 = goodMorning(‘Bob’);
goodMorningAlert2();
In the example above, when goodMorningAlert2() will be executed, you will see output as an alert with “Good Morning Bob”.
In short, in JavaScript, if you use the function keyword inside another function, you are creating a closure.