Do you know what will be the output of this code?
JavaScript
const counter = (function () {
let count = 0;
return { inc: () => ++count };
})();
counter.inc();
console.log(counter.count);
Show answer and explanation
The answer is undefined.
count is a local variable of the immediately invoked function, not a property of the returned object. Only inc can reach it through its closure. That’s the module pattern for private state.