Demystifying JavaScript Hoisting: A Comprehensive Guide with Examples

Sumeet Panchal
3 min readJun 6, 2023

--

Introduction:

JavaScript is a versatile programming language known for its flexible and sometimes surprising behaviors. One such behavior is hoisting, which can catch developers off guard if they are not familiar with it. In this blog post, we will dive into the concept of hoisting in JavaScript, explain how it works, and provide illustrative examples to enhance your understanding. By the end, you’ll be equipped with the knowledge to navigate hoisting and write more reliable JavaScript code.

Understanding Hoisting:

Hoisting is a mechanism in JavaScript where variable and function declarations are moved to the top of their respective scopes during the compilation phase. This means that regardless of where variables and functions are declared in your code, they are effectively treated as if they were declared at the top of their scope.

However, it is important to note that only the declarations are hoisted, not the initializations. Variables are initially assigned the value `undefined` until they are explicitly assigned a value.

Hoisting with Variable Declarations:

Let’s explore how hoisting works with variable declarations. Consider the following code snippet:

console.log(myVariable); // Output: undefined
var myVariable = 10;
console.log(myVariable); // Output: 10

At first glance, one might expect the first `console.log` statement to throw a `ReferenceError` since `myVariable` is accessed before it is declared. However, due to hoisting, the code is effectively interpreted as follows:

var myVariable;
console.log(myVariable); // Output: undefined
myVariable = 10;
console.log(myVariable); // Output: 10

As you can see, the declaration `var myVariable` is moved to the top of its scope, resulting in the first `console.log` statement printing `undefined` instead of throwing an error.

Hoisting with Function Declarations:

Hoisting also applies to function declarations. Consider the following example:

sayHello(); // Output: Hello!
function sayHello() {
console.log(‘Hello!’);
}

Even though the `sayHello()` function is called before it is defined, JavaScript hoists the function declaration to the top, making it accessible throughout its scope. This code is effectively interpreted as:


function sayHello() {
console.log(‘Hello!’);
}
sayHello(); // Output: Hello!

Hoisting ensures that functions declared using the `function` keyword are available to be called anywhere within the scope, regardless of their actual placement in the code.

Hoisting Does Not Apply to Function Expressions:

It’s important to note that hoisting only works with function declarations, not function expressions. Take a look at the following example:


sayHello(); // Output: TypeError: sayHello is not a function
var sayHello = function() {
console.log('Hello!');
};

In this case, the variable `sayHello` is hoisted, but its value is assigned `undefined` rather than a function. Therefore, when the code tries to execute `sayHello()`, it results in a `TypeError` because `sayHello` is not a function.

Conclusion:

JavaScript hoisting can be both useful and confusing, especially for developers who are not familiar with its behavior. Understanding hoisting is crucial for writing predictable and maintainable code. Remember that hoisting applies to both variable and function declarations but not to function expressions.

By grasping the concepts presented in this blog post and practicing with the provided examples, you’ll be better equipped to handle hoisting in your JavaScript projects. Embrace hoisting as a powerful feature of the language and use it wisely to optimize your code and enhance readability.

Happy coding!

--

--

Sumeet Panchal
Sumeet Panchal

Written by Sumeet Panchal

Programming enthusiast specializing in Android and React Native, passionate about crafting intuitive mobile experiences and exploring innovative solutions.

No responses yet