Programming Languages
JavaScript
Subjective
Sep 25, 2025
What is closure in JavaScript?
Detailed Explanation
A closure is a function that has access to variables in its outer (enclosing) scope even after the outer function has returned. It gives you access to an outer function's scope from an inner function.
Example:
function outerFunction(x) {
return function innerFunction(y) {
return x + y;
};
}
const addFive = outerFunction(5);
console.log(addFive(3)); // 8
Here, innerFunction has access to the variable x from outerFunction even after outerFunction has finished executing.
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts