Question 43
Question
Answer
function Counter() {
let count = 0; // Private variable accessible only within the closure
return {
increment: function() {
count++;
},
getCount: function() {
return count;
}
};
}
const myCounter = Counter();
console.log(myCounter.getCount()); // Output: 0
myCounter.increment();
console.log(myCounter.getCount()); // Output: 1Last updated