Source Code
JavaScript receives the file exactly as you wrote it.
Understand how JavaScript prepares variables and functions before your code starts executing.
Learn what actually happens during JavaScript's Memory Creation Phase, why var behaves differently from let and const, how function declarations are hoisted, and how to avoid common interview mistakes.
Estimated Time
13 minutes
Difficulty
Beginner
Progress
0% Complete
Imagine JavaScript reads your entire file before running it. During this first pass, it reserves memory for variables and functions. That preparation process is called Hoisting.
The important part: JavaScript prepares declarations first, but it still executes your code line by line. Assignments do not jump to the top.
What Hoisting is
Memory Creation Phase
Execution Phase
Variable Hoisting
Function Hoisting
Temporal Dead Zone
var vs let vs const
Function Declaration vs Function Expression
Common Interview Questions
Hoisting makes more sense when you separate setup from execution.
JavaScript receives the file exactly as you wrote it.
JavaScript reserves names for variables and functions before running code.
JavaScript runs line by line and updates values.
Console logs, returned values, and errors appear from executed lines.
Step through source code, memory, call stack, console output, and execution notes.
1console.log(a);23var a = 10;45console.log(a);
Before any line runs, JavaScript sees var a and stores a with the value undefined.
All three declarations are prepared, but they are not prepared in the same way.
Memory
undefined
Access
Can access before declaration
Output
undefined
Memory
Memory reserved
Access
Cannot access before declaration
Output
ReferenceError
Memory
Memory reserved
Access
Cannot access before declaration
Output
ReferenceError
The variable name exists, but JavaScript blocks access until the declaration executes.
1console.log(userName);23let userName = "Zubair";45console.log(userName);
Current phase
Access here throws ReferenceError because userName is still inside the Temporal Dead Zone.
The word function is not enough. The declaration style decides what JavaScript stores in memory.
1sayHello();23function sayHello() {4console.log("Hello");5}
Memory
sayHello: Function()
Output
Hello
Function declarations are stored in memory as complete functions during the memory creation phase.
Hoisted
Works before declaration
Not callable before initialization
Acts like the variable used to store it
Behaves like let or const
Cannot run before initialization
Temporal Dead Zone
Name exists, but cannot be used early
This is the mental picture to keep during interviews.
a
undefined
var declaration
b
TDZ
let declaration
c
TDZ
const declaration
sayHello()
Function()
function declaration
Switch between examples, step through execution, and inspect memory and console output.
1console.log(score);2var score = 42;3console.log(score);
Click Run to see output.
var is hoisted and initialized with undefined, then updated when the assignment runs.
Most hoisting bugs come from mixing up prepared memory with real assigned values.
Using let before declaration and expecting undefined.
Using const before declaration and expecting undefined.
Thinking variables physically move to the top of the file.
Confusing undefined with ReferenceError.
Calling function expressions before initialization.
Hoisting is not just an interview topic. It affects startup order, modules, utilities, and React files.
Declare config before use so startup code never depends on accidental hoisting behavior.
Read environment values into const variables before passing them into services.
Initialize application state before functions begin consuming it.
Imports are prepared before module code runs, which is why they appear available at the top level.
Function declarations make reusable components and helpers easier to call from nearby code.
Function declarations are useful when you want helpers to be readable below the main flow.
React components are often function declarations because they are named, reusable, and easy to read in stack traces.
Hooks cannot be called conditionally because React depends on the same call order during every render.
Import declarations are processed before module execution, so imported values are available throughout the module.
Each answer includes a simple explanation, detailed explanation, interview answer, example, memory trick, and common mistake.
Simple explanation
Hoisting is JavaScript preparing variable and function names before code starts running.
Detailed explanation
During memory creation, JavaScript scans the code and creates memory slots for declarations. var starts as undefined, function declarations become full functions, and let, const, and class declarations stay unavailable until initialized.
Interview answer
Hoisting is JavaScript's creation-phase behavior where declarations are registered before execution. The behavior differs for var, let, const, functions, and classes.
Real example
console.log(a); var a = 10; logs undefined first.
Memory trick
JavaScript prepares names first, then runs lines.
Common mistake
Saying declarations physically move to the top.
Fifteen interview-style questions with explanations and why each wrong option fails.
Progress
0/15 answered
A final comparison table for quick revision before interviews.
Always declare variables before using them.
Prefer const by default.
Use let only when reassignment is required.
Avoid var in modern JavaScript.
Prefer function declarations for reusable utilities that benefit from being callable early.
Use clear declaration order and deliberate function styles in real projects.
1const apiBaseUrl = process.env.NEXT_PUBLIC_API_URL;23function createApiUrl(path: string) {4return `${apiBaseUrl}${path}`;5}
Configuration is declared before helpers use it, so readers do not need to reason about hoisting.
1const total = calculateCartTotal(items);23function calculateCartTotal(items) {4return items.reduce((sum, item) => {5return sum + item.price * item.quantity;6}, 0);7}
A function declaration can be called before it appears, which can keep the high-level flow readable.
1const featureFlags = loadFeatureFlags();23if (featureFlags.enableCheckout) {4initializeCheckout();5}
Declare and initialize values before branching on them.
1const formatPrice = (amount) => {2return new Intl.NumberFormat("en-US", {3style: "currency",4currency: "USD",5}).format(amount);6};78console.log(formatPrice(49));
Function expressions and arrow functions are excellent after initialization.