#47MediumQuiz
47. Why is it, in general, a good idea to leave the global JavaScript scope of a website as-is and never touch it?
JavaScript
Polluting the global scope causes variable collisions between scripts, harder debugging, and unintended memory retention.
Example 1:
const counter = createScopedCounter(0);
counter.increment(); // 1
counter.increment(); // 2
"count" in globalThis; // falsesolution.js
Counter instances maintain isolated state
(() => {
const a = createScopedCounter(0);
const b = createScopedCounter(10);
expect(a.increment()).toBe(1);
expect(a.increment()).toBe(2);
expect(b.getValue()).toBe(10);
})()