How would you use an IIFE to avoid polluting the global namespace in a script?
You would avoid polluting the global namespace by wrapping all code in an IIFE so variables are scoped to the function. Prior to ES6 modules and block-scoped variables (let/const), this was the primary way to prevent variables from leaking into the global scope. By enclosing all your code in an IIFE, any variables declared inside it remain private to that function scope. This was a common practice in larger JavaScript applications and libraries, where preventing name collisions was crucial. For example, jQuery and many other libraries use this pattern to ensure their internal variables don't conflict with other scripts on the page. Even with modern JavaScript, the pattern remains useful for isolating code execution.