Learn hoisting in JavaScript with clear examples and explanations. Understand variable hoisting in JavaScript, function hoisting in JavaScript, and how the temporal dead zone affects hoisting in JS.
Anuj Sharma
Last Updated Sep 14, 2025
Advertisement
If you're preparing for frontend interviews, understanding Hoisting in JavaScript is a must. It's one of those concepts that seems simple at first glance but can trip you up during coding rounds or real-world debugging. In this blog, we’ll break down hoisting in JavaScript in an easy-to-understand way, with examples and explanations that make sense even if you're just getting started.
Let's deep dive into what it is, how it works internally, and how different types of variables and functions behave because of hoisting.
In simple terms, hoisting in JS is a behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase, before the code is executed.
This means that you can use a variable or call a function before you’ve actually declared it in the code!
For example:
console.log(myVar); // undefined
var myVar = 5;
Here, myVar is declared after it’s used, but because of variable hoisting in JavaScript, the declaration is moved to the top and only the assignment happens in place.
Another example with functions:
greet();
function greet() {
console.log("Hello!");
}
This works perfectly because of function hoisting in JavaScript. The entire function definition is hoisted to the top of the scope.
To understand hoisting in JavaScript, you need to understand that JavaScript has two phases when executing code:
Creation phase – where the memory space is allocated for variables and functions, and they are initialized (either to undefined
or fully loaded depending on their type).
Execution phase – where the code runs line by line and the assignments or function calls are processed.
During the creation phase, the JavaScript engine scans the code and moves declarations (but not initializations) to the top. This is why scope plays a huge role in how hoisting behaves.
For example, in the global context, all var
declarations are hoisted and initialized with undefined. At the same time,
let
and const
are hoisted but not initialized, they are in a temporal dead zone until the code reaches their definition.
Inside a function, the same process happens, but in the local function scope.
var is hoisted and initialized with undefined
.
let and const are hoisted but not initialized. Trying to access them before their declaration throws a ReferenceError because they are in the Temporal Dead Zone (TDZ).
The TDZ is the time between the entering of the scope and the actual variable declaration where the variable cannot be accessed. This helps prevent bugs and makes code more predictable.
console.log(globalVar); // undefined
var globalVar = "I'm global";
console.log(globalVar); // I'm global
console.log(globalLet); // ReferenceError
let globalLet = "I'm global";
console.log(globalConst); // ReferenceError
const globalConst = "I'm global";
function testVar() {
console.log(innerVar); // undefined
var innerVar = "Inside function";
console.log(innerVar); // Inside function
}
testVar();
function testLetConst() {
console.log(innerLet); // ReferenceError
let innerLet = "Inside function";
console.log(innerConst); // ReferenceError
const innerConst = "Inside function";
}
testLetConst();
Unlike variables, function declarations are hoisted with their entire body.
sayHello();
function sayHello() {
console.log("Hello from a function declaration!");
}
The function can be called before it is declared because it’s fully hoisted.
Arrow functions, however, behave differently because they are treated as variable assignments, not function declarations.
sayHi(); // TypeError: sayHi is not a function
const sayHi = () => {
console.log("Hello from an arrow function!");
};
Here, sayHi
is hoisted but remains uninitialized until the assignment happens, leading to a runtime error if accessed before its definition.
// Function Declaration
greetUser(); // Works!
function greetUser() {
console.log("Welcome, user!");
}
// Arrow Function
greetAdmin(); // TypeError
const greetAdmin = () => {
console.log("Welcome, admin!");
};
When you assign a function to a variable, it's treated like variable hoisting only, and all the rules applied to the var, let and const are similarly applied to the Function Variables. Checkout the example below to understand function variable hoisting in javascript.
// Using var
console.log(varFunc()); // undefined
var varFunc = function() {
return "I'm a function expression!";
};
// Using let
console.log(letFunc()); // ReferenceError
let letFunc = function() {
return "I'm a function expression!";
};
// Using const
console.log(constFunc()); // ReferenceError
const constFunc = function() {
return "I'm a function expression!";
};
In this example, only varFunc is hoisted (with undefined), but letFunc and constFunc are hoisted but uninitialized, leading to a runtime error if accessed too early.
By mastering hoisting in JavaScript, you'll easily handle tricky interview questions and write cleaner, more predictable code in your day-to-day work as a frontend developer. Keep experimenting with examples, and you'll soon feel comfortable explaining these concepts to anyone.
Advertisement
Advertisement
Advertisement
Vijay Sai Krishna vsuri
Last Updated Aug 21, 2025
A Quick guide about popstate event in JavaScript, If you’ve ever hit the back button in your browser and wondered how your Single-Page Application knows which view to render, this guide is for you.
Alok Kumar Giri
Last Updated Jun 2, 2025
Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.
Anuj Sharma
Last Updated Aug 3, 2025
Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.
Anuj Sharma
Last Updated Jun 27, 2025
A detailed list of 20+ most asked Frontend Machine Coding Interview Questions and resources both in JavaScript & React. Also covers expected functional/Non-functional requirements in this Interview.
Anuj Sharma
Last Updated Sep 6, 2025
A comprehensive cheat sheet for the Frontend Machine Coding Interview Round, which helps to revise all the important machine coding & UI design concepts before the interview.
Kirtesh Bansal
Last Updated Aug 31, 2025
A beginner-friendly guide to understanding call, apply, and bind methods in JavaScript, along with step-by-step call, apply and bind polyfill implementations that are often asked in interviews.
Subscribe to FrontendGeek Hub for the frontend interview preparation, interview experiences, curated resources and roadmaps.
© 2024 FrontendGeek. All rights reserved