JavaScript Closures
JavaScript variables can be local or global.
Private variables can use closures.
Global Variables
A function can access variables defined inside the function, such as:
Example
function
myFunction
(
)
{
var
a
=
4
;
return
a
a
;
}
Try it »
A function can also access variables defined outside the function, such as:
Example
var
a
=
4
;
function
myFunction
(
)
{
return
a
a
;
}
Try it »
In the latter example, a is a Global variable.
In a web page, global variables belong to the window object.
Global variables can be used by all scripts on the page.
In the first example, a is a local variable.
A local variable can only be used inside the function that defines it. It is not available to other functions or script code.
Even if a global and a local variable have the same name, they are two different variables. Modifying one does not affect the value of the other.
![]() |
If a variable is declared without using var the keyword, then it is a global variable, even if it is defined inside a function. |
|---|
Variable Lifecycle
The scope of a global variable is global, meaning that throughout the entire JavaScript program, the global variable is everywhere.
Variables declared inside a function only work inside the function. These variables are local variables with local scope; function parameters are also local and only work inside the function.
The Counter Dilemma
Imagine if you want to count some values, and the counter is available in all functions.
You can use a global variable, and a function sets the counter to increment:
Example
var
counter
=
0
;
function
add
(
)
{
return
counter
+=
1
;
}
add
(
)
;
add
(
)
;
add
(
)
;
//
The counter is now 3
Try it »

The counter value changes when the add() function is executed.
But here is the problem: any script on the page can change the counter, even without calling the add() function.
If I declare the counter inside a function, then without calling the function I cannot modify the counter's value:
Example
function
add
(
)
{
var
counter
=
0
;
return
counter
+=
1
;
}
add
(
)
;
add
(
)
;
add
(
)
;
//
The intention was to output 3, but contrary to expectations, the output is 1 every time!
Try it »

The above code will not output correctly; every time I call the add() function, the counter is set to 1.
** JavaScript nested functions can solve this problem.**
JavaScript Nested Functions
All functions can access global variables.
In fact, in JavaScript, all functions can access the scope one level above them.
JavaScript supports nested functions. A nested function can access the variables of the function one level above.
In this example, the nested function plus() can access the parent function's counter variable:
Example
function
add
(
)
{
var
counter
=
0
;
function
plus
(
)
{
counter
+=
1
;
}
plus
(
)
;
return
counter
;
}
Try it »
If we could access from the outside plus() the function, this would solve the counter dilemma.
We also need to ensure that counter = 0 executes only once.
We need a closure.
JavaScript Closures
Remember function self-invocation? What does this function do?
Example
var
add
=
(
function
(
)
{
var
counter
=
0
;
return
function
(
)
{
return
counter
+=
1
;
}
}
)
(
)
;
add
(
)
;
add
(
)
;
add
(
)
;
//
The counter is 3
Try it »
Example Parsing
Variables add Specifies the return value of the function's self-invocation.
The self-invoking function executes only once. It sets the counter to 0 and returns the function expression.
The add variable can be used as a function. The great part is that it can access the counter in the scope one level above the function.
This is called a JavaScript **closure.** It makes it possible for a function to have private variables.
The counter is protected by the scope of the anonymous function and can only be modified through the add method.
![]() |
A closure is a mechanism for protecting private variables. It creates a private scope when the function executes, thereby protecting the internal private variables from outside interference. Intuitively, a closure is like a stack environment that will not be destroyed. |
|---|
