Understanding JavaScript Closures with outer() and inner() Function Example
JavaScript closures are one of the most powerful — and sometimes confusing — concepts in the language. Whether you’re preparing for a job interview or just learning JavaScript, closures are essential to understand.
In this blog post, we’ll break down closures using a simple, real-world example involving an outer() and inner() function. By the end, you’ll not only understand how closures work but also why they’re so useful in JavaScript.
JavaScript Closure Example
Let’s start with the following code snippet:
function outer() {
let count = 0;
function inner() {
count++;
console.log(count);
}
return inner;
}
const fn = outer();
fn(); // ?
fn(); // ?
What’s Happening Here?
To understand this, let’s walk through the code line-by-line.
1. function outer() {…}
This defines a function called outer. Inside it:
- A variable
countis declared and initialized to0. - Another function
inner()is defined, which incrementscountand logs it.
2. return inner;
The outer() function returns the inner() function, not by calling it, but by passing the function definition itself.
3. const fn = outer();
Here’s where it gets interesting:
- We call
outer()and assign the result (which is theinnerfunction) to the variablefn. - At this point,
fnis now a reference to the inner function, but it’s also linked to a preserved scope wherecount = 0.
This is what we call a closure in JavaScript:
A closure is a function that remembers the variables from its outer lexical scope, even after that scope has closed.
4. Calling fn();
fn(); // Outputs: 1
fn(); // Outputs: 2
Each time you call fn():
- The
countvariable is still available from theouter()function’s scope. count++increments the value.- It logs the updated value to the console.
Even though outer() has finished executing, its local variables live on — thanks to closures.
Multiple Instances with Their Own Closure
If you do this:
const anotherFn = outer();
anotherFn(); // Outputs: 1 (separate count!)
You’ll notice that a new closure is created. The new call to outer() makes a new count variable in memory, completely independent from the first one.
Why Use Closures in JavaScript?
Closures allow for:
- Data encapsulation (private variables)
- Factory functions
- Event handlers and callbacks
- Memoization and caching
- Currying and partial application
Simulating Private Variables in JavaScript
JavaScript doesn’t have traditional access modifiers like private, but closures can simulate privacy:
function createCounter() {
let count = 0;
return function() {
count++;
return count;
};
}
const counter = createCounter();
console.log(counter()); // 1
console.log(counter()); // 2
Here, count is inaccessible directly, but counter() can still modify and use it — this is a powerful pattern for secure and maintainable code.
Key Takeaways
- Closures allow functions to access variables from their outer scope even after that scope has returned.
- Every call to a closure-generating function creates a new, independent environment.
- Closures are essential for stateful functions, private data, and modular JavaScript design.
SEO Keywords Targeted in This Article
- What is a closure in JavaScript?
- JavaScript outer and inner functions example
- How closures work in JavaScript
- JavaScript closure interview question
- JavaScript private variables using closure
Conclusion
Closures are everywhere in JavaScript — from array methods to async functions and libraries like React. Understanding them is essential for writing clean, efficient, and secure code.
If you’ve ever been confused by nested functions and variable scoping, remember: closures are simply functions bundled with their environment
