Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
The call stack is a data structure that tracks function calls in your code. Think of it as a stack of plates - you can only add or remove from the top. When a function is called, it's pushed onto the stack. When it finishes, it's popped off. This is how JavaScript knows where to return after a function completes.
When a function is called, JavaScript creates a stack frame containing:
This frame is pushed onto the top of the stack.
JavaScript executes the code inside the function at the top of the stack. If this function calls another function, that new function is pushed on top.
When a function finishes (returns), its stack frame is popped (removed) from the stack. Control returns to the function below it.
This process repeats until the stack is empty. The program is complete when all functions have returned and the stack is clear.
The call stack has a maximum size. If you keep pushing functions without popping them (usually due to infinite recursion), you'll get a Stack Overflow error.
function recursiveLoop() {
recursiveLoop(); // Calls itself forever!
// Never returns, keeps pushing frames
}
recursiveLoop();
// Error: Maximum call stack size exceededfunction countdown(n) {
if (n === 0) return; // Base case - stops recursion
console.log(n);
countdown(n - 1); // Eventually hits base case
}
countdown(5); // Safely counts down: 5, 4, 3, 2, 1Last In, First Out
Push on call, pop on return
Like a stack of plates
Contains function info
Arguments, variables, return address
Created per function call
Too many function calls
Usually from infinite recursion
Always have base cases!
DevTools shows call stack
See function call chain
Helps trace execution flow