
Hoisting is a concept which enables us to extract values of variables and functions even before initialising/assigning value without getting error and this is happening due to the 1st phase (memory creation phase) of the Execution Context.
So in previous lecture, we learnt that execution context gets created in two phase, so even before code execution, memory is created so in case of variable, it will be initialized as undefined while in case of function the whole function code is placed in the memory
Hoisting in JavaScript is a behavior where variable and function declarations are moved to the top (not physically but due to creation and execution phase) of their during the compilation phase, before code execution begins. This process is not magic but a systematic setup that allows certain variables and functions to be accessible before their actual declaration in the code.
Only declarations are hoisted, not initializations. This that variables declared with var are hoisted as undefined, and functions declared with function declarations are fully hoisted with their entire body available from the start. We will see that by example's.
Before execution, JavaScript performs a creation phase where it scans the code to allocate memory and set up the environment:
console.log(magicNumber); // undefined
var magicNumber = 42
function abracadabra() {
return "Magic!";
}
During the creation phase, JavaScript interprets this as:

var magicNumber; // hoisted as undefined
function abracadabra() {} // fully hoisted
Similar to an showing up early but not knowing their lines, var variables are hoisted as undefined. They are declared but uninitialized until assignment occurs during execution.
console.log(actor); // undefined
var actor = "Ready to perform!";
console.log(actor); // "Ready to perform!"
console.log(mysterious) // ReferenceError
let mysterious = "I'm here!";
ReferenceError. We get reference error because let and const variable are in Temoral Dead Zone during the creation phase.
{
console.log(x); // ❌ ReferenceError (TDZ)
let x = 10;
console.log(x); // ✅ 10
}
const y; // ❌ SyntaxError: Missing initializer
Function declarations are the main actors, arriving fully prepared and available from the start. Function expressions, however behave like variables and follow var hoisting rules, resulting in undefined if called before assignment.
performMagic(); // "Abracadabra!"
function performMagic() {
return Abracadabra!";
}
tryMagic(); // TypeError
var tryMagic = function() {
return "Maybe magic?";
};
var a = 1;
function test() {
console.log(a); // ?
var a = 2;
console.log(a); // ?
}
test();
Answer First console.log outputs undefined because var a inside test is hoisted as undefined. The second outputs .
// Function declaration
console.log(add(2, 3)); // 5
function add(a, b) {
return a + b;
}
// Function expression
console.log(subtract(5, 2)); // Error
var subtract = function(a, b) {
return a - b;
};
Function declarations are fully hoisted, but function expressions are hoisted undefined, leading to errors if called before assignment.
JavaScript's execution involves creating a global execution context (GEC) and local execution for functions. During creation:
During execution, code runs line by line, and assignments happen at runtime. Local contexts are created for functions, with their own scope and variables. Global Environment and the window Object. Even an empty JavaScript program creates the global execution context, which includes the window object in browsers. This object contains all variables and functions, accessible via window or this at the global level. Variables declared globally with var are attached to window. example:
var x = 10;
console.log(this.x); // 10
console.log(window.x); // 10
When a variable is declared but not assigned a value, it is undefined. If a variable is not declared at all, trying to access it results in a ReferenceError with message "not defined".
console.log(x); // undefined
var x = 25;
console.log(a); // ReferenceError: a is not defined
JavaScript is loosely typed, so variables change types dynamically. Never manually assign undefined to variables; let JavaScript handle it.
let and const are hoisted but in the Temporal Dead Zone until initialized. Accessing them before initialization causes ReferenceError.
console.log(a); // ReferenceError
let a = 10;
console.log(b); // undefined
var b = 15;
Attempting to redeclare let or const in same scope results in syntax errors. const must be initialized at declaration and cannot be reassigned.
Initially, hoisting seemed like a confusing JavaScript quirk. However, recognizing it as part of the creation phase of execution clarifies that JavaScript systematically sets up the environment before running code. The "magic" is just JavaScript showing you how it prepares everything in advance.

I'm Rahul, Sr. Software Engineer (SDE II) and passionate content creator. Sharing my expertise in software development to assist learners.
More about me