Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Function Hoisting

Earlier in this tutorial, you learned about “hoisting” (JavaScript Hoisting).

Hoisting is JavaScript’s behavior of moving declarations to the top of the current scope.

This applies to both variable and function declarations, allowing JavaScript functions to be called before they are declared.

myFunction(5);

function myFunction(y) {
  return y * y;
}

Functions defined using an expression are not hoisted.

Self-Invoking Functions

Function expressions can be “self-invoking,” meaning they execute automatically without explicit calls when followed by (). However, function declarations cannot be self-invoked. To distinguish a function expression, it must be enclosed in parentheses.

Example

(function () {
  let x = “Hello!!”;  // I will invoke myself
})();

The function above is an anonymous self-invoking function, meaning it has no name and is executed automatically.