DEV Community

Cover image for All About JavaScript Hoisting
Muhammad Shakir
Muhammad Shakir

Posted on • Edited on

All About JavaScript Hoisting

Hoisting is a fundamental concept in JavaScript that involves the way variables and function declarations are treated during the compilation process. This article aims to provide a comprehensive understanding of hoisting, including its behavior with different variable types, the concept of Temporal Dead Zone (TDZ), function hoisting, and debunking common misconceptions.

"During the compilation phase, JavaScript places all declarations, such as variables and functions, into memory. This process is commonly known as hoisting. However, it's important to note that only the declarations themselves are hoisted, not their actual assignments or initializations."

Behavior of Different Variable Types:

var: hoisted and initialized with undefined.

let and const: hoisted but NOT INITIALIZED with undefined.

EXPLAINATION: Var can be accessed even BEFORE IT IS INITIALIZED while let and const can only be accessed AFTER INITIALIZATION. If we try to access let and const before initialization, it will give a Reference Error.

Consider the following Examples:

console.log(a);  // undefined
console.log(b); // Reference Error
console.log(c); // Reference Error
var a=1;
let b=2;
const c=3
Enter fullscreen mode Exit fullscreen mode

Why Reference Error: This is because of TDZ (Temporal Dead Zone).

What is Temporal Dead Zone?

Before diving deep into TDZ, let's understand few terms:

BLOCK: a block is a pair of braces ({ . . . lines of code . . . }) in which a statement or group of statements are wrapped.

Consider the following Examples:

{ 
  console.log(a);
  var a;
}

Enter fullscreen mode Exit fullscreen mode

INITIALIZATION: When you assign value to a variable.

Consider the following Examples:

{
var a;  // variable declaration
a=10 // initialization of a variable
}

Enter fullscreen mode Exit fullscreen mode

Now what exactly a TDZ is?

Its an area of the block where a variable is inaccessible until your computer completely initialize it.

If you try to access a variable before its complete initialization, JavaScript will throw a Reference Error because the variable is in its temporal dead zone. So, to prevent JavaScript from throwing a Reference Error we will have to remember to access the variable outside of its temporal dead zone.

Function Hoisting:

In addition to variable hoisting, JavaScript also hoists function declarations. Function hoisting allows you to call functions before they are actually declared in the code.

Consider the following example:

sayHello(); // Output: "Hello!" 
function sayHello() { 
console.log("Hello!"); 
}
Enter fullscreen mode Exit fullscreen mode

In this case, even though the function sayHello() is called before its actual declaration, JavaScript hoists the function declaration to the top of the current scope. As a result, the function can be invoked successfully, and the output will be "Hello!".

However, it's important to note that function expressions do not get hoisted in the same way as function declarations. For example:

sayHi(); // Throws a TypeError: sayHi is not a function 
var sayHi = function() { 
console.log("Hi!"); 
};
Enter fullscreen mode Exit fullscreen mode

In this case, the variable sayHi is hoisted and initialized with the value undefined, but the assignment of the function expression to the variable remains in the original position. Therefore, when we try to invoke sayHi() before the actual assignment, we get a TypeError stating that sayHi is not a function.

To ensure proper function hoisting, it's recommended to use function declarations instead of function expressions if you need to call the function before its declaration.

Myths about Hoisting in JavaScript?

Variables and function declarations are moved physically to the top of the code.

Fact about this myth:

during compilation of the code variables and function declarations put into memory and stay exactly where they are typed.

Conclusion:

Hoisting is a crucial mechanism in JavaScript that dictates how declarations are processed during compilation. Understanding the behavior of different variable types, the Temporal Dead Zone, and function hoisting is essential for writing clean and error-free JavaScript code. By dispelling common misconceptions about hoisting, developers can enhance their grasp of this core concept.

Top comments (0)