Write a function that creates and returns a closure which keeps track of how many times it has been called.
JavaScript automatically manages memory through garbage collection, which frees memory occupied by objects that are no longer accessible.
Memory leaks occur when variables or objects are unintentionally retained in memory.
A closure keeps references to variables, which can affect memory if not managed carefully.
function createCounter() {
let count = 0;
return function() {
count++;
return count;
};
}
const counter = createCounter();
console.log(counter()); // 1
console.log(counter()); // 2