Menu

Understanding JavaScript Hoisting: Variables and Functions Explained

Understanding JavaScript Hoisting: Variables and Functions Explained

JavaScript is an essential language for web development, but some of its behaviors can be a bit tricky to understand at first. One such behavior is hoisting—a concept that often confuses beginners. In this article, I'll try and unravel the mystery of hoisting, focusing on how it affects variables and functions in JavaScript. 

By the end of this guide, you’ll have a solid grasp of how hoisting works and how it influences your code’s execution.

What Is Hoisting?

In simple terms, hoisting is JavaScript’s default behavior of moving declarations to the top of their respective scope during the compile phase. This means that functions and variables appear to be “hoisted” to the top of the code, even before the execution starts.

But there’s a catch to this whole process. Hoisting works differently for variables and functions. And even for the different types of variable declarations and function declarations. Understanding this distinction is crucial for writing predictable and error-free JavaScript code.

So let's take a look at each individual scenario.

Hoisting with Variables

When it comes to variables, the behavior of hoisting differs based on the type of variable declaration you use: var, let, or const.

1. Hoisting with var

The var keyword is the most traditional way to declare variables in JavaScript, but it comes with its own quirks. When a variable is declared using var, the declaration is hoisted to the top of its scope, but its initialization remains in place. This can lead to some unintuitive behavior.

Consider this example:

console.log(a); // Output: undefined
var a = 5;
console.log(a); // Output: 5

Even though the console.log(a) call occurs before the declaration, JavaScript doesn’t throw an error. This happens because the declaration var a; is hoisted to the top of the scope, meaning that it is indeed declared at this early point. However, the assignment (a = 5;) stays in place, so the variable is undefined at the time of the first log statement.

This is important to remember if you are into the habit of declaring variables throughout your codebase and not at the very top.

2. Hoisting with let and const

In ES6 (ECMAScript 2015), JavaScript introduced let and const for variable declarations. These keywords behave differently compared to var in terms of hoisting.

Both let and const are hoisted as well, but they are not initialized until the code reaches their actual line. Until then, they are in the so-called "temporal dead zone" (TDZ), meaning any attempt to access them before declaration results in a ReferenceError.

Here’s an example:

console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 10;

With let and const, the hoisting still occurs, but the variable remains uninitialized in the temporal dead zone until its actual declaration line.

Hoisting with Functions

JavaScript also hoists function declarations, but the way it handles them is different from variables.

1. Function Declarations

When you define a function using a function declaration, both the function’s name and body are hoisted to the top of the scope. This allows you to call the function even before you define it in your code.

For example:

sayHello(); // Output: Hello, World!

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

In this case, the entire function sayHello is hoisted to the top, allowing it to be called before its definition in the code. This is why you don’t encounter an error when calling sayHello() early.

2. Function Expressions

Hoisting works differently for function expressions however. In function expressions, the function is assigned to a variable, and only the variable name is hoisted, not the function itself.

Consider the following example:

sayHi(); // TypeError: sayHi is not a function

var sayHi = function() {
  console.log("Hi!");
};

Here, the variable sayHi is hoisted, but it’s undefined at the time of the function call. This is why calling sayHi() before the assignment results in a TypeError. Essentially, function expressions behave similarly to variables declared with var.

Hoisting with Arrow Functions

Arrow functions, introduced in ES6, are a special type of function expression. Like regular function expressions, they are not fully hoisted. Only the variable declaration is hoisted, leaving the function itself uninitialized until its line is reached.

Here’s an example:

console.log(sayHi); // Output: undefined
sayHi(); // TypeError: sayHi is not a function

var sayHi = () => {
  console.log("Hi!");
};

Just like traditional function expressions, arrow functions exhibit similar hoisting behavior, meaning you can't call them before their definition.

Avoiding Hoisting Pitfalls

While hoisting can be useful, it can also lead to confusing bugs if you're not careful. Here are a few tips to avoid common pitfalls:

Use let and const over var: Since let and const provide better scoping and avoid the undefined behavior of var, they are generally recommended for variable declarations.

Declare functions before usage: Although function declarations are hoisted, it’s good practice to declare functions before you call them. This helps make your code more readable and avoids reliance on hoisting.

Beware of function expressions and arrow functions: If you’re using function expressions or arrow functions, make sure to define them before trying to call them, as these are not fully hoisted like function declarations.

Understanding Scope and Hoisting Together

Hoisting doesn’t happen in isolation. It’s closely tied to scope. In JavaScript, you have two main types of scope:

Global scope: Variables and functions declared outside of any function are in the global scope and can be accessed from anywhere in the code.

Function scope: Variables declared inside a function are only accessible within that function.

It’s essential to understand that hoisting moves declarations to the top of their respective scope. If a variable is declared inside a function, it gets hoisted to the top of that function’s scope, not the global scope.

Code Example: Putting It All Together

Let’s look at a more complex example to see how hoisting, variable declarations, and scope interact.

function hoistingExample() {
  console.log(x); // undefined
  var x = 10;
  
  console.log(y); // ReferenceError: Cannot access 'y' before initialization
  let y = 20;

  function hoistedFunction() {
    console.log("I am hoisted!");
  }

  hoistedFunction(); // Output: I am hoisted!

  notHoisted(); // TypeError: notHoisted is not a function

  var notHoisted = function() {
    console.log("I am not hoisted!");
  };
}

hoistingExample();

In this example:

The var x declaration is hoisted, but its assignment happens later, leading to undefined when we first log x.

The let y is in the temporal dead zone, so accessing it before initialization results in a ReferenceError. 

The hoistedFunction is a function declaration, so it’s fully hoisted and can be called before it’s defined.

The notHoisted function expression is only partially hoisted—its variable declaration is hoisted, but the function itself is not, leading to a TypeError when we try to call it early.

Conclusion

Hoisting is a fundamental concept in JavaScript, and understanding how it works is key to writing clean, predictable code. While hoisting can simplify certain coding practices, it can also lead to unexpected behaviors, especially with variable declarations.

Walter G. author of blog post
Walter Guevara is a Computer Scientist, software engineer, startup founder and previous mentor for a coding bootcamp. He has been creating software for the past 20 years.

Get the latest programming news directly in your inbox!

Have a question on this article?

You can leave me a question on this particular article (or any other really).

Ask a question

Community Comments

No comments posted yet
Ad Unit

Current Poll

Total Votes:
Q:
Submit

Add a comment