Explore the 5 practical examples to understand closures in JavaScript that can help to use closures in the application confidently.
Anuj Sharma
Last Updated Feb 17, 2026

Understanding closures in JavaScript is crucial for frontend developers as they play a significant role in how functions work and interact with variables. This is one of the confusing topics with so many applications in different contexts.
In this blog post, we will explore 5 practical examples to help you grasp the concept of Closures in JS and use them confidently in your applications.
In JavaScript, a closure is a combination of a function bundled together with references to its surrounding state (the lexical environment). This means that a closure gives you access to an outer function's scope from an inner function. Here is a simple example.
function outerFunction() {
let outerVariable = 'I am from the outer function';
function innerFunction() {
console.log(outerVariable);
}
return innerFunction;
}
const innerFn = outerFunction();
innerFn(); // Output: I am from the outer function
In this example, the inner function has access to the outerVariable defined in the outer function, demonstrating how closures work.
function addClickListener() {
let count = 0;
document.getElementById('clickMeButton').addEventListener('click', function() {
count++;
console.log('Button clicked ' + count + ' times');
});
}
addClickListener();
Here, the closure maintains access to the count variable across multiple click events, incrementing it each time the button is clicked.
function delayedGreeting() {
let name = 'Alice';
setTimeout(function() {
console.log('Hello ' + name);
}, 2000);
}
delayedGreeting();
This example showcases how closures capture the name variable even after the outer function has finished executing, allowing the delayed function to access it.
function multiplier(factor) {
return function(number) {
return number * factor;
};
}
const double = multiplier(2);
console.log(double(5)); // Output: 10
By returning a function inside another function, closures enable the creation of functions that remember and use the variables from their lexical environment.
function createCounter() {
let count = 0;
return {
increment: function() {
count++;
},
getCount: function() {
return count;
}
};
}
const counter = createCounter();
counter.increment();
console.log(counter.getCount()); // Output: 1
This example demonstrates how closures can be used to encapsulate and protect data, providing a way to maintain private variables within a function.
Closures are a powerful feature in JavaScript that can be leveraged to write more concise and efficient code. By mastering closures, you can enhance your understanding of functions and scope in JavaScript.
Understanding different use cases of closures in JavaScript helps to easily answer closure-related questions in the frontend interview and also helps in day-to-day frontend development work.
A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me 🚀
Be the first to share your thoughts!
No comments yet.
Start the conversation!
Build Your Portfolio
Help the Community
Strengthen Your Skills
Share your knowledge by writing a blog or quick notes. Your contribution can help thousands of frontend developers ace their interviews and grow their careers! 🚀
Alok Kumar Giri
Last Updated Jun 2, 2025
Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.
Anuj Sharma
Last Updated Oct 2, 2025
Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.
Anuj Sharma
Last Updated Nov 24, 2025
Understand step by step how to flatten nested array in javascript using recursion, also explore the flatten of complex array of object.
Anuj Sharma
Last Updated Nov 23, 2025
Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.
Anuj Sharma
Last Updated Aug 3, 2025
Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.
Anuj Sharma
Last Updated Oct 26, 2025
Understand the step-by-step implementation of Infinite Currying Multiplication in JavaScript with a code example.
Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.
All in One Preparation Hub to Ace Frontend Interviews. Master JavaScript, React, System Design, and more with curated resources.
© 2026 FrontendGeek. All rights reserved