A disclaimer first: This post will make sense to you only if you have read and understood my post on lexical scoping in JavaScript .


And if you have read my post then i don’t have much to explain here.

Now, since JavaScript is lexically scoped, the following script is perfectly valid:


function localise(greeting) {

return function(name) { alert(greeting + ‘ ‘ + name };


var english = localise(‘Hello’);

var french = localise(‘Bonjour’);

english(‘kartik’);                                 // alert box with “Hello kartik”

french(‘kartik’)                                   // alert box with “Bonjour kartik”


Here, we have defined a parent function called localise which returns an anonymous inner function. This inner function remembers the variable greeting value even when at runtime the variable does not exist.

This is because when the inner function is defined, the greeting variable was part of the scope.

We say that the inner function is closed over the containing scope, or for short, that the inner function is a closure.

In other words, the result is that the procedure body is always evaluated in the context of the environment that was current when the procedure was created.


This is an extremely powerful functional language feature, but it is important to not misuse it. There are ways to cause memory-leak-like situations using closures. to know more about how closures can cause memory leaks, refer to a post by Eric Lippert at