DEV Community

Cover image for Hoisting in javaScript
Muhammed Shameel Maravan
Muhammed Shameel Maravan

Posted on

Hoisting in javaScript

JavaScript is a versatile and widely used programming language. However, it has some quirks that can sometimes puzzle beginners. One of these quirks is hoisting. Don't worry; it's not as complicated as it sounds! In this blog post, I'll break down the concept of hoisting in JavaScript in simple terms, and I'll provide plenty of code examples to illustrate how it works.

What is Hoisting?

Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase, but their assignments stay in place. This means that you can use a variable or a function before it's declared in your code, but there are some caveats you should be aware of.

Variable Hoisting:

Let's start by looking at variable hoisting. Consider the following code snippet:

console.log(name);//The output will be undefined
var name = "Shameel";

Surprisingly, this code will not result in an error. Instead, it will output undefined to the console. This is because the variable declaration var name is hoisted to the top of its scope, but the assignment name = "Alice" stays in place.

Here's how the code is interpreted by JavaScript:

var name; // Declaration is hoisted
console.log(name); // Outputs undefined
name = "Shameel"; // Assignment stays in place

Function Hoisting:

Hoisting also applies to function declarations. Take a look at this example:

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

In this case, the sayHello function is called before it's declared, but it still works. JavaScript hoists the function declaration to the top of the scope, so it's available when the code is executed.

Hoisting with let and const:

Hoisting behaves differently with let and const compared to var. Variables declared with let and const are hoisted to the top of their block, but they are not initialized until they reach their declaration in the code. This behavior is often referred to as the "temporal dead zone."

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

Conclusion:

Understanding hoisting is crucial for writing clean and predictable JavaScript code. Remember these key points:

Variable and function declarations are hoisted to the top of their containing scope.

Variable assignments stay in place.

Function declarations are fully hoisted, and you can call them before declaring them.

Be cautious when using let and const, as they are also hoisted but have a temporal dead zone.

By grasping the concept of hoisting and practicing with code examples, you'll become a more confident JavaScript developer.

Try It Yourself!
The best way to solidify your understanding of hoisting is to experiment with code. Feel free to copy the examples provided here and run them in your own JavaScript environment. Tinker with the code, modify variables and observe how hoisting behaves. Don't hesitate to reach out if you have any questions or run into any challenges.

❤️Happy coding❤️

Top comments (0)