Click here to Skip to main content
15,790,565 members
Articles / Programming Languages / Javascript

Closures in JavaScript

Rate me:
Please Sign up or sign in to vote.
3.71/5 (27 votes)
12 Jul 2012CPOL2 min read 46.6K   26   10
An introduction to closures in JavaScript.


Definition - A closure is a function together with a referencing environment for the non-local variables of that function. << refer to link  >>

In JavaScript, using simple terms,  if we create a function inside another function, we are creating a closure. 

In most common languages, after a function returns, all the local variables are no longer accessible because the stack-frame is destroyed.  Keeping this in mind a closure can be considered as a stack-frame which is not deallocated when the function returns. 

Why use Closure

I have many asking me why closures are required. I will try to explain.

The scenario where I came across closures was while using Tabular Controls, File Objects etc. With Closures I could store the page indexes, row counts etc that I could re use for other client functions. With FSO i could have file paths , counts etc saved for later use. Its not only for complex client controls. Think of scenarios where your page has many controls and you are performing client side validations. Each time you acces a control by


the whole page control tree is being searched. This can be avoided by using closures and saving the controls just once. Re-usability is a key advantage of closures.  Hope I have convinced few to atleast get to know Closures.

Using the code  

Take a look at the function below:

function sayHello(name) {
    var text = 'Hello ' + name; // local variable
    var sayAlert = function() { alert(text); }
    return sayAlert;

Now we call the function in the following way:

var say2 = sayHello("Sachin");

The function sayHello() returns a pointer to the function sayAlert().

Here when you declare a function within another function, then the local variables can remain accessible after returning from the function you called. This is demonstrated above; we call the function say2() after we have returned from sayHello(). Notice that the code that we call references the variable text, which was a local variable of the function sayHello().

Consider the function below:

function setupSomeGlobals() {

    // Local variable that ends up within closure
    var num = 666;

    // Store some references to functions as global variables
    gAlertNumber = function() { alert(num); }
    gIncreaseNumber = function() { num++; }
    gSetNumber = function(x) { num = x; }


The three functions gAlertNumber(), gIncreaseNumber(), and gSetNumber(x) have shared access to the same closure - the local variables of setupSomeGlobals() when the three functions were defined.

This can be checked by calling the functions one after the other.

Try the below sequence:

setupSomeGlobals() //value of num is 666 
gAlertNumber () //alerts 666 

gAlertNumber () //alerts 667 
gSetNumber (600) 
gAlertNumber () //alerts 600

If we call setupSomeGlobals() again, then a new closure (stack-frame!) is created. The old gAlertNumber, gIncreaseNumber, gSetNumber variables are overwritten with new functions that have the new closure.


  • A closure in JavaScript is like keeping a copy of all the local variables, just as they were when a function exited.
  • It is probably best to think that a closure is always created just on the entry to a function, and the local variables are added to that closure.
  • A new set of local variables is kept every time a function with a closure is called (given that the function contains a function declaration inside it, and a reference to that inside function is either returned or an external reference is kept for it in some way).


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralMy vote of 4 Pin
Santhakumar Munuswamy @ Chennai23-May-15 9:59
professionalSanthakumar Munuswamy @ Chennai23-May-15 9:59 
GeneralMy vote of 5 Pin
Prasad Khandekar23-May-13 4:50
professionalPrasad Khandekar23-May-13 4:50 
GeneralMy vote of 4 Pin
germ1316-Jul-12 18:10
germ1316-Jul-12 18:10 
SuggestionMy vote of 5, but... a couple of notes Pin
Sergey Alexandrovich Kryukov28-Jun-12 13:32
mvaSergey Alexandrovich Kryukov28-Jun-12 13:32 
GeneralRe: My vote of 5, but... a couple of notes Pin
SachinKumarK12-Jul-12 4:36
SachinKumarK12-Jul-12 4:36 
QuestionWhy? Pin
Fred_Smith26-May-07 8:20
Fred_Smith26-May-07 8:20 
AnswerRe: Why? Pin
RichardGrimmer29-May-07 0:15
RichardGrimmer29-May-07 0:15 
GeneralRe: Why? Pin
Sergey Alexandrovich Kryukov28-Jun-12 13:27
mvaSergey Alexandrovich Kryukov28-Jun-12 13:27 
GeneralRe: Why? Pin
TheFisch16-Jul-12 11:37
TheFisch16-Jul-12 11:37 
AnswerRe: Why? [modified] Pin
SachinKumarK31-May-07 23:04
SachinKumarK31-May-07 23:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.