re: ELI5: Why use a function declaration, expression, or an IIFE in JavaScript? VIEW POST

re: Yes, you are correct that an IIFE creates its own scope. As a consequence of that, any variable declared inside an IIFE is only visible to its scop...

The one place where IIFEs can still be useful is surprisingly async/await in Node.

You can't use await outside of async so you end up doing something like:

(async () => {
  try {
    await something()
  } catch(e) {

That's unless you have top-level await support in your environment (which Node doesn't but the esm package can be configured to provide or if you transpile)


Oh, yeah. How could I forget about that?

Also, just a side note, I just realized now that top-level await is essentially a glorified way of writing top-level synchronous code. That's kind of strange... 🤔

code of conduct - report abuse