FunctionsCannotBeImmediatelyInvoked
DRAFT

Misconception:

A function can be called only if it was previously stored in a variable.

Incorrect

Functions cannot be called in the expression in which they are defined

Correct

Functions can be immediately called in the expression in which they are defined

Correction
Here is what's right.

There are two ways to define a function:

  • through a function declaration:

      function foo(){}; // function declaration
      () => {} // arrow function declaration
  • through a function expression:

      // function expressions
      (function foo(){});
      const foo = function(){};
      const bar = { foo: function(){} }
    
      // arrow function expressions
      (() => {});
      const foo = () => {};
      const bar = { foo: () => {} }

Function declarations cannot be immidiately invoked:

Function expressions can be invoked as they are defined:

Difficult cases

Internal scope

Function expressions can be named, meaning that it is possible to access the function from within its own scope:

Function expressions are not function declarations

A function expression does not declare the function in the containing scope of the expression.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.