DEV Community

Cover image for Hoisting in JavaScript: Understanding the Inner Workings
koushikmaratha
koushikmaratha

Posted on

Hoisting in JavaScript: Understanding the Inner Workings

Hoisting is a fundamental concept in JavaScript, but it is not always easy to understand. In the basic version of hoisting, variable and function declarations are moved to the top of their respective scopes. However, in advanced hoisting, we dive deeper into the inner workings of JavaScript and discover some more complex behaviors.

In this article, we will explore some of the advanced hoisting features in JavaScript, including block-level hoisting, hoisting with let and const, and hoisting with classes.

Block-level Hoisting

In ES6, the introduction of let and const keywords changed the way variables are declared and hoisted. Unlike the var keyword, let and const are block-scoped and cannot be accessed outside of their block.

When a variable is declared using let or const, it is not hoisted to the top of its scope. Instead, the variable declaration remains in its original location within the block. This means that if you try to access a let or const variable before it has been declared, you will get a ReferenceError.

Consider the following example:


{
  console.log(foo); // ReferenceError: foo is not defined
  let foo = "bar";
}

Enter fullscreen mode Exit fullscreen mode

In this code, the variable foo is declared using let within a block. When we try to log the value of foo before it has been declared, we get a ReferenceError. This is because let and const variables are not hoisted to the top of their block.

Hoisting with let and const

Although let and const variables are not hoisted to the top of their scope, they are still hoisted in a sense. When a variable is declared using let or const, it is allocated memory during the creation phase of the JavaScript engine.

The variable is initialized with a value of undefined, just like a variable declared with var. However, unlike var, the variable cannot be accessed before it is declared. This is because let and const variables are not accessible until their declaration has been processed.

Consider the following example:


console.log(foo); // ReferenceError: Cannot access 'foo' before initialization
let foo = "bar";

Enter fullscreen mode Exit fullscreen mode

In this code, we try to log the value of foo before it has been initialized. However, we get a ReferenceError because the variable cannot be accessed before its initialization.

Hoisting with Classes

ES6 introduced the class keyword, which provides a more familiar syntax for creating objects in JavaScript. However, like functions, classes are also subject to hoisting.

When a class is defined, the entire class declaration is hoisted to the top of its scope. This means that you can create a new instance of a class before its declaration in your code.

Consider the following example:


const foo = new Foo();

class Foo {
  constructor() {
    console.log("Hello, world!");
  }
}

Enter fullscreen mode Exit fullscreen mode

In this code, we create a new instance of the class Foo before it has been declared. However, because classes are hoisted to the top of their scope, this code will work as expected.

Conclusion

Advanced hoisting in JavaScript can be a tricky concept to understand, but it is an essential part of becoming a proficient JavaScript developer. By understanding the inner workings of hoisting, you can write more efficient and effective code that takes advantage of this powerful feature.

Top comments (0)