JavaScript Hoisting Guide For a Beginner

In JavaScript, Hoisting is one of the confusing concepts for beginners. In this post, I’ll explain hoisting with examples to make it easy to understand.

Let’s jump right into it.🚀

First things first, let’s start by understanding hoisting.

What is JavaScript Hoisting?

Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of the current script or the current function during the compilation phase.

This means you can use variables and functions in your code even before they are declared.

Note: Only the declarations are hoisted, not the initializations (means their values).

Let’s understand this with some examples.


Variable Hoisting

console.log(x);
var x = 13;

Here, we are trying to console.log the value of x before its declaration, so it should give us an error.

Let’s run this code and see what happens.

JavaScript Hoisting

This gives undefined not any error. But why does this happen?

This happens because of the hoisting.

In the console.log, the declaration of x is hoisted to the top with an initial value of undefined. So it doesn’t give the error.

Now let’s take another example.

console.log(x);
var x = 13;
console.log(x);

Output:

Variable Hoisting

In the first console.logx is hoisted to the top with an initial value of undefined and in the second console.log, after the declaration, the value is now 13.

To put it simply, hoisting means the declaration of both variables and functions is moved to the top of your code behind the scenes. So, even if you write your code in a certain order, JavaScript rearranges it during its compilation phase.

What if we use let or const instead of var?

console.log(x);
let x = 10;

Output:

JavaScript Hoisting

Here, we get a ReferenceError.

Now, let’s see this with const.

console.log(x);
const x = 10;

Output:

Variables declared with let and const are hoisted, but they are not initialized with undefined like var variables. So using them before declaration results in a ReferenceError. This is known as the “temporal dead zone.”


JavaScript Hoisting with Function Declarations

greet();

function greet() {
  console.log("Hello, World!");
}

Output:

Function Declarations

Here, the function greet is hoisted to the top, so we can call it before its declaration.


JavaScript Hoisting with Function Expressions

greet();

var greet = function() {
  console.log("Hello, World!");
};

Output:

Hoisting with Function Expressions

Here, only the variable greet is hoisted, not its value. So, when you try to call it before the assignment, it throws an error.

Note: Function declarations are fully hoisted, while function expressions are not.

In summary, if you’re using a variable or function before declaring it, don’t worry, JavaScript will know about it and won’t throw an error. But keep in mind that only the names are hoisted (moved up), not their values.

To put it simply, It’s like telling your friend about a plan but not the details of the plan. They’ll know there’s something, but they won’t know the details.


Best Practices

To avoid any confusions or errors, it’s a good practice to declare and initialize variables at the top of their respective scopes.

// Good Practice
var x = 13;
console.log(x); // Output: 13

// Avoid
console.log(x); // undefined
var x = 13;

That’s all for today!

If you’re new to web development, check out Learnify — my curated platform with beginner-friendly tutorials to help you learn web development step-by-step with examples and simple explanations.

If you enjoy my work and want to support what I do:

👉 Become a Patreon supporter
👉 Or buy me a coffee

Every small gesture keeps me going! 💛

Follow me on X (Twitter) to get daily web development tips & insights.


Enjoyed reading? You may also find these articles helpful.

Lexical Scope and Closures in JavaScript

Type Coercion in JavaScript Explained

5 thoughts on “JavaScript Hoisting Guide For a Beginner”

  1. Does yοur ԝebsite have a contact page?
    I’m having problems locating it but, I’d like to send you an email.
    I’ve gߋt some creative ideas fоr your blog you might be interested in һearing.
    Either way, great Ƅlog and I loоk forward to seeing it develop over time.

    Reply

Leave a Comment