JavaScript
Closures in JavaScript: Explained with Examples
2/27/2025
5 min read

Learn how closures work in JavaScript and why they are powerful.
Closures in JavaScript: Explained with Examples
Description:
A closure is a function that remembers the variables from its outer scope even after the function execution is complete.
Explanation:
Closures allow functions to access variables outside their local scope. This is useful for data encapsulation and maintaining state across function calls.
For example:
function outerFunction(outerVariable) {
return function innerFunction(innerVariable) {
console.log(`Outer: ${outerVariable}, Inner: ${innerVariable}`);
};
}
const newFunction = outerFunction("Hello");
newFunction("World"); // Output: Outer: Hello, Inner: World
The innerFunction still remembers outerVariable even after outerFunction has executed, demonstrating closure behavior.









