Introduction
The closure function is part of advanced JavaScript. It is used to access outer function parameters and variables in the inner function, so the inner function is called a closure.
The inner function returns to the outer function.
What is a Closure?
A closure is a function that remembers and can access variables from its outer (parent) scope, even after the outer function has finished executing.
In simple words:
A closure “closes over” the variables of its parent function.
- Use closures for private variables.
- Use let instead of var in loops.
- Release references when not needed.
Basic Example
function outer() {
let message = "Hello";
function inner() {
console.log(message);
}
return inner;
}
const func = outer();
func();
Output:
Note: Even though outer() has finished execution, inner() still remembers the message.
Example 2:
Suppose you have an outer function start() and declared the employeeName variable in this, and called this variable in the displayEmployeeName() inner function.
function start() {
var employeeName = 'John';
function displayEmployeeName() {
console.log("Hello "+employeeName);
}
displayEmployeeName();
}
start();
Output:
Example 3:
Suppose you change the outer scope variable x value in the inner function scope, then the value will be changed.
// declaration in outer scope
var x = 5;
function getNum() {
console.log(x); // outer scope is captured on declaration
}
getNum();
Output:
Example 4:
Suppose you change the outer scope variable x value in the inner function scope, then the value will be changed.
var x = 5; // declaration in outer scope
function getNum() {
x=2;
console.log(x); // outer scope is captured on declaration
}
getNum();
Output:
How Closures Work
Closures rely on lexical scope, which means functions remember where they were defined.
You will see the steps of Closure work.
- A function is created inside another function
- The inner function accesses variables from the outer function
- The inner function is returned or used later
- The variables remain accessible even after the outer function execution
Real-Life Examples
You will see some Real Life Examples.
1. Counter Function
Example:
function counter() {
let count = 0;
return function() {
count++;
return count;
};
}
const c = counter();
console.log(c());
console.log(c());
Output:
2
2. Multiplication of Numbers
function multiplier(factor) {
return function(num) {
return num * factor;
};
}
const double = multiplier(2);
console.log(double(5));
Output:
Closure vs Global Variables
Closures provide a safer alternative to global variables.
| Feature | Closure | Global Variable |
|---|---|---|
| Scope | Private | Public |
| Security | High | Low |
| Maintainability | Better | Poor |
Common Mistakes
Many Beginners do below common mistakes.
1. Confusing Scope
function test() {
let x = 10;
}
console.log(x);
Output:
2. Loop Issues
for (var i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}
Output:
3
3
✔ Fix:
for (let i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}
Output:
1
2
Advantages of Closures
There are some advantages of Closures
- Data privacy
- Persistent state
- Cleaner code
- Functional programming support
- Reusability
Disadvantages of Closures
There are some disadvantages of Closures
- Memory consumption
- Harder to debug for beginners
- Overuse can complicate code
Javascript Closure function – Interview Questions
Q 1: What is closure?
Q 2: Why closures are used?
Q 3: Do closures store variables?
Q 4: Are closures memory-heavy?
Q 5: Example use?
Javascript Closure function – Objective Questions (MCQs)
Q1. A closure is created when a function ______.
Q2. Closures allow access to ______ variables.
Q3. Closures are commonly used for ______.
Q4. Variables in closures are preserved in ______.
Q5. Which feature enables closures?
Conclusion
Closures are one of the most powerful features in JavaScript that allow functions to remember and access variables from their outer scope. They play a crucial role in modern JavaScript development, enabling patterns like data encapsulation, memoization, and function factories.