Reading — step 1 of 5
Learn
~1 min readScope and Closures
An IIFE (Immediately-Invoked Function Expression) is a function that runs as soon as it's defined:
js
The wrapping parens are needed to make the parser treat it as an expression. Without them, JavaScript thinks function() {} is a declaration and complains.
IIFEs were the classic way to create private scope before ES6 modules. The module pattern:
js
In modern code, ES modules (import/export) replaced this — but you'll still see IIFEs in legacy code, bundles, and some library code.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…