Question 42
Question
Answer
function outerFunction(x) {
let y = 10; // Variable in outer function's scope
function innerFunction() {
console.log(x + y); // Accesses 'x' and 'y' from outer scope
}
return innerFunction;
}
const myClosure = outerFunction(5); // Call outerFunction, creating the closure
myClosure(); // Outputs: 15 (accessing x=5 and y=10) Last updated