What is Hoisting in JavaScript ?

Introduction

In the world of JavaScript, hoisting is a term you’ll often come across. It’s a behavior of JavaScript’s execution context that moves variable and function declarations to the top of their containing scope before code execution.

Detailed Explanation

To understand hoisting, we need to understand how JavaScript reads our code. When the JavaScript engine first reads the code, it goes through a phase known as the creation phase. In this phase, the engine scans the entire code for variable and function declarations and places them into memory. This is what we refer to as “hoisting”.

However, it’s crucial to note that only the declarations are hoisted, not the initializations. If a variable is declared and initialized after using it, the variable will be hoisted and initialized with a value of undefined.

Let’s look at some examples to understand this better:

Example 1. Variable Hoisting

console.log(myVar); // Output: Uncaught ReferenceError: myVar is not defined
var myVar = 5;
console.log(myVar); // Output: 5

Here’s what’s happening.

  1. First Line (console.log(myVar);): At this point, myVar has been declared due to hoisting, but it has not been initialized yet. In JavaScript, when a variable is declared but not initialized, its value is undefined. That’s why console.log(myVar); outputs Uncaught ReferenceError: myVar is not defined.

  2. Second Line (var myVar = 5;): Here, myVar is initialized with the value 5.

  3. Third Line (console.log(myVar);): Now, when we log myVar to the console, it outputs 5, the value we just assigned to it.

So, even though myVar is used before the line where it seems to be declared, JavaScript’s hoisting feature means the declaration (var myVar;) is actually processed at the start of the scope, while the initialization (myVar = 5;) happens in-place. This is why we get undefined for the first log and 5 for the second log.

Example 2. Function Hoisting

Function declarations are also hoisted in JavaScript, which means they can be called before they are declared in the code.

hoistedFunction(); // Output: "This function has been hoisted."

function hoistedFunction() {
  console.log("This function has been hoisted.");
}

In this example, calling hoistedFunction() before its declaration in the code does not result in an error because the function declaration is hoisted.

Hoisting is a fundamental concept in JavaScript that can lead to unexpected results if not understood correctly. It’s essential for every JavaScript developer to understand how hoisting works to avoid common pitfalls and write cleaner, more predictable code.

Conclusion

While hoisting can seem confusing at first, understanding it can greatly improve your JavaScript coding skills. It’s a powerful part of the language that, when leveraged correctly, can make your code more efficient and easier to read. Remember, a good grasp of hoisting and scope can set you apart in the JavaScript world.

I hope you will find this article helpful. If you have any suggestions, then please feel free to ask in the comment section.

Thank you.