Hoisting

May 20, 2023

Hoisting is a mechanism in JavaScript that allows variable and function declarations to be moved to the top of their respective scopes at runtime. This means that regardless of where a variable or function is declared within a scope, it can be used anywhere within that scope, even before its declaration.

Background

In JavaScript, variable and function declarations are parsed before any code is executed. This means that regardless of where a variable or function is declared within a scope, it is available for use anywhere within that scope. This behavior is known as hoisting.

How Hoisting Works

Hoisting works by moving variable and function declarations to the top of their respective scopes. This process happens during the compilation phase of JavaScript, before any code is executed.

Variable Hoisting

When a variable is declared using the var keyword, it is automatically hoisted to the top of its scope. For example, the following code:

function hoistingExample() {
  console.log(message);
  var message = "Hello, world!";
}

is interpreted by JavaScript as:

function hoistingExample() {
  var message;
  console.log(message);
  message = "Hello, world!";
}

This means that even though message is declared after the console.log statement, it is still available for use within that statement.

Function Hoisting

Function declarations are also hoisted to the top of their respective scopes. For example, the following code:

function hoistingExample() {
  sayHello();

  function sayHello() {
    console.log("Hello, world!");
  }
}

is interpreted by JavaScript as:

function hoistingExample() {
  function sayHello() {
    console.log("Hello, world!");
  }

  sayHello();
}

This means that even though sayHello is defined after it is called within the hoistingExample function, it is still available for use within that function.

Hoisting Exceptions

There are some exceptions to hoisting that are important to be aware of. One of these exceptions is that variable assignments are not hoisted. For example, the following code:

function hoistingExample() {
  console.log(message);
  var message = "Hello, world!";
  console.log(message);
}

is interpreted by JavaScript as:

function hoistingExample() {
  var message;
  console.log(message);
  message = "Hello, world!";
  console.log(message);
}

This means that the first console.log statement will output undefined, because message has been hoisted to the top of the scope, but has not yet been assigned a value.

Another exception to hoisting is that function expressions are not hoisted. For example, the following code:

function hoistingExample() {
  sayHello();

  var sayHello = function() {
    console.log("Hello, world!");
  }
}

will result in a TypeError because sayHello is not a function. This is because function expressions are not hoisted, only function declarations are.

Why Hoisting is Useful

Hoisting can be a useful feature of JavaScript because it allows developers to write code in a more natural and intuitive way. For example, it allows them to use a function before it is defined, which can make code easier to read and understand.

Hoisting can also be helpful in preventing errors. For example, if a variable is used before it is declared, hoisting will ensure that it is still available for use, even though it has not yet been declared.