Skip to content
Discussion options

You must be logged in to vote

A closure is a function that remembers the variables from its outer scope even after the outer function has finished executing. This allows functions to maintain private variables.

javascript
Copy
Edit
function outerFunction(outerVariable) {
return function innerFunction(innerVariable) {
console.log(Outer: ${outerVariable}, Inner: ${innerVariable});
};
}

const closureExample = outerFunction("Hello");
closureExample("World"); // Output: Outer: Hello, Inner: World

Closures are commonly used in data hiding, function factories, and event handlers.

Replies: 10 comments 8 replies

Comment options

You must be logged in to vote
3 replies
@arafat-fullstackdev
Comment options

@arafat-fullstackdev
Comment options

@patwaryh
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by arafat-fullstackdev
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@patwaryh
Comment options

Comment options

You must be logged in to vote
1 reply
@FrankFarabi
Comment options

Comment options

You must be logged in to vote
1 reply
@arafat-fullstackdev
Comment options

Comment options

You must be logged in to vote
1 reply
@FrankFarabi
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@arafat-fullstackdev
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants