Skip to content

Latest commit

 

History

History
19 lines (15 loc) · 406 Bytes

IIFE.md

File metadata and controls

19 lines (15 loc) · 406 Bytes

Explain why the following doesn't work as an IIFE

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.

  function foo() {
    // code
  }();

Just put parentheses:

  ( function foo() {
    //code
  } )();
// JS interpret everything what is in parentheses as `expression` not a `statement`.
// So, it will run immediately