JavaScript Coding Day 11 | Hoisting in JavaScript – A Complete Beginner-Friendly Guide
Hoisting in JavaScript – A Complete Beginner-Friendly Guide
Watch the lesson tutorial 🔻
Hoisting is one of the most misunderstood concepts in JavaScript. If you are learning JS, you must understand how variable and function declarations behave before the code is executed.
This article explains hoisting in a simple way, with clear examples, expected outputs, and explanations.
🔍 What Is Hoisting?
Hoisting means JavaScript moves all variable and function declarations to the top of the scope during the compile phase — before the code runs.
This means you can sometimes use a function or variable before it is declared in the code. However, different types of variables behave differently.
🧪 Example 1: Hoisting with var
console.log(x);
var x = 10;
✔ Output:
undefined
✔ Why?
The declaration
var xis hoisted (moved to the top).The value (
10) is not hoisted.JavaScript treats the code like this:
JavaScriptvar x; // Declaration moved to top console.log(x); // undefined x = 10; // Assignment stays here
🧪 Example 2: Hoisting with let
console.log(y);
let y = 20;
❌ Output:
ReferenceError: Cannot access 'y' before initialization
✔ Why?
letis hoisted but placed in the Temporal Dead Zone (TDZ).You cannot access the variable before the specific line where it is declared.
🧪 Example 3: Hoisting with const
console.log(z);
const z = 30;
❌ Output:
ReferenceError: Cannot access 'z' before initialization
✔ Why?
constbehaves exactly likelet.It is hoisted but initialized only at the line of declaration.
It stays in the Temporal Dead Zone until the code reaches the declaration.
🧪 Example 4: Function Declarations Are Fully Hoisted
greet();
function greet() {
console.log("Hello World!");
}
✔ Output:
Hello World!
✔ Why?
Function declarations are fully hoisted.
Both the function name and the body are moved to the top.
You can call them safely before writing them in your code.
🧪 Example 5: Function Expressions Are Not Fully Hoisted
sayHello();
var sayHello = function() {
console.log("Hello!");
};
❌ Output:
TypeError: sayHello is not a function
✔ Why?
Only the variable declaration
var sayHellois hoisted.The function assignment is not hoisted.
During execution, JavaScript sees this:
JavaScriptvar sayHello; // undefined sayHello(); // undefined() → TypeError sayHello = function() { ... }
🧪 Example 6: Arrow Functions with let / const
welcome();
const welcome = () => {
console.log("Welcome!");
};
❌ Output:
ReferenceError
✔ Why?
The variable
welcomeis in the Temporal Dead Zone until its line is reached.You cannot call it before declaration.
📌 Quick Summary Table
| Type | Hoisted? | Value Hoisted? | Access Before Declared? |
| var | Yes | No | ✔ Allowed (undefined) |
| let | Yes | No | ❌ Error (TDZ) |
| const | Yes | No | ❌ Error (TDZ) |
| Function Declaration | Yes | Yes | ✔ Allowed |
| Function Expression | Yes (as var) | No | ❌ Error |
| Arrow Function | Yes (as var) | No | ❌ Error |
🎯 Final Summary
Hoisting is a mechanism where JavaScript moves all declarations to the top of their scope. However:
var→ Hoisted and initialized asundefined.letandconst→ Hoisted but not initialized (stuck in Temporal Dead Zone).Function declarations → Fully hoisted (safe to use before declaring).
Function expressions / Arrow functions → Behave like variables; they are not hoisted with their value.
Understanding hoisting helps you avoid bugs and write cleaner, safer JavaScript code.
- by Chirana Nimnaka

Comments
Post a Comment