Programming Languages JavaScript Subjective
Sep 18, 2025

Explain what a closure is in JavaScript and provide an example.

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
Feedback