What is hoisting in javascript and how it works?

Hoisting in JavaScript is a concept where variable and function declarations are moved to the top of their containing scope during the compile phase. This process allows developers to use functions and variables before they are declared in the code. Understanding hoisting in JavaScript is crucial for writing efficient and bug-free scripts, as it directly impacts how code is interpreted by the JavaScript engine.

What is Hosting in JavaScript?


Hoisting in JavaScript refers to the behavior where variable and function declarations are automatically moved to the top of their scope (either global or local) before the code is executed. This means that a function or a variable can be used before it is declared in the script. For example:

code
console.log(x); // Output: undefined
var x = 5;
Even though the console.log(x); statement declares the variable x after it, there is no error in the example above. Instead, JavaScript hoists the declaration of x to the top, but not the assignment. This is why the output is undefined rather than a ReferenceError.

How Does Hosting in JavaScript Work?


To understand how hoisting in JavaScript works, it’s important to differentiate between “declarations” and “initializations.” During the compile phase, JavaScript scans through the code, looks for variable and function declarations, and moves them to the top of their respective scopes. However, initializations or assignments do not get hoisted.

For example:

code
console.log(jsHoist); // Output: undefined
var jsHoist = “Hoisting in JS”;
console.log(jsHoist); // Output: Hoisting in JS
In the above code, the declaration var jsHoist is hoisted to the top of the scope, but the assignment jsHoist = “Hoisting in JS” remains in its original position. This is why the first console.log(jsHoist) outputs undefined and not “Hoisting in JS.”.

Function Hoisting in JavaScript


Function declarations are also affected by hoisting in JavaScript. However, unlike variables, functions are hoisted along with their definitions. 
This allows you to invoke a function even before it has been declared in the code.

code
jsHoistFunction(); // Output: “Function hoisting in JavaScript works!”

function JSHoistFunction() {
console.log(“Function hoisting in JavaScript works!”);
}
In the example above, the function jsHoistFunction is hoisted to the top of its scope along with its definition. This is why calling the function before its declaration works without any issues.

Hoisting in JS with Function Expressions


Function expressions, however, do not enjoy the same behavior as function declarations. When a function is defined using a variable (function expression), only the variable declaration is hoisted, not the function definition.

code
jsHoistExpression(); // Output: TypeError: jsHoistExpression is not a function

var jsHoistExpression = function () {
console.log(“This will not hoist.”);
};
In this case, hoisting in JS only hoists the variable jsHoistExpression, not the function definition. As a result, when we try to invoke JSHoistExpression before its definition, JavaScript throws a TypeError saying it’s not a function.

Hoisting JS and let and const


ES6 introduced new keywords like let and const, which behave differently in terms of hoisting in JavaScript. Although JavaScript hoists these variables, it does not initialize them with undefined like variables declared with var. Instead, they stay in a “temporal dead zone” from the start of the block until declared.

code
console.log(jsLetHoist); // Output: ReferenceError: Cannot access ‘jsLetHoist’ before initialization

let jsLetHoist = “Hoisting in JS with let”;

Here, LetHoist js is not accessible before its declaration, resulting in a ReferenceError. This behavior makes the scope of let and const more predictable and helps prevent common issues related to hoisting in JS.

Best Practices to Handle Hosting in JavaScript


To effectively manage hoisting in JavaScript and avoid unexpected behaviors, consider the following best practices:

Declare variables at the top:

To avoid confusion, always declare variables at the top of their scope. This approach makes the code more readable and minimizes the effect of hoisting in JavaScript.

Use let and const instead of var. Since let and const do not initialize to undefined and stay in a temporal dead zone until declared, they prevent many common pitfalls associated with hoisting in JS.

Avoid using variables before declaration:

Always try to declare variables and functions before using them. This habit can help avoid errors and make the code easier to understand.

Use function expressions judiciously:

Keep in mind that JavaScript does not hoist function expressions like function declarations. If you intend to call a function before its definition, use a function declaration instead.

Common Pitfalls Related to Hoisting in JavaScript


Hoisting in JavaScript can lead to several common pitfalls, especially for developers unfamiliar with its workings:

Undefined Variables:

Since JavaScript hoists var declarations but doesn’t initialize them, using a variable before assigning a value leads to undefined instead of an error. This behavior can lead to unforeseen bugs in the code.

Temporal Dead Zone with let and const:

With let and const, hoisting leads to a temporal dead zone. Attempting to access the variable before its declaration will throw a ReferenceError.

Function Expression Confusion:

New developers may mistakenly think that JavaScript hoists function expressions like function declarations. Misunderstanding this difference can result in unexpected type error exceptions.

Conclusion


Understanding hoisting in JavaScript is vital for any developer looking to write clean and effective code. While JavaScript hoists declarations to the top of their scope, it does not hoist js initializations or function expressions. By being mindful of these details, developers can avoid common pitfalls and harness the power of hoisting in JS for more predictable and maintainable code.

For more topics on javaScript click here

Leave a Comment