0
0
Javascriptprogramming~10 mins

Stack overflow concept in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Stack overflow concept
Function call starts
Push call frame on stack
Execute function body
Function calls itself (recursion)
Push new call frame on stack
Repeat until stack limit reached
Stack overflow error thrown
Program crashes or stops
When a function keeps calling itself without stopping, each call adds a new frame to the stack. If this goes on too long, the stack runs out of space and causes a stack overflow error.
Execution Sample
Javascript
function recurse(n) {
  if (n === 0) return 0;
  return recurse(n - 1) + 1;
}
recurse(100000);
This code calls a function recursively many times until n reaches 0, but with a very large input it may cause a stack overflow.
Execution Table
StepCall Depth (n)ActionStack SizeResult/Output
1100000Call recurse(100000)1No output yet
299999Call recurse(99999)2No output yet
399998Call recurse(99998)3No output yet
...............
999991Call recurse(1)99999No output yet
1000000Call recurse(0), base case reached100000Return 0
1000011Return from recurse(1)99999Return 1
1000022Return from recurse(2)99998Return 2
...............
Stack limit reachedToo deepStack overflow error thrownStack max sizeProgram crashes
💡 Stack limit reached because too many recursive calls filled the call stack
Variable Tracker
VariableStartAfter 1After 2After 3...Final
n100000999999999899997...0
Key Moments - 3 Insights
Why does the stack size keep increasing with each recursive call?
Each recursive call adds a new frame to the call stack to keep track of that call's variables and where to return. This is shown in the execution_table rows 1 to 100000 where stack size grows.
Why does the program crash instead of finishing?
Because the recursion is too deep, the stack runs out of space and throws a stack overflow error before reaching the base case, as shown in the last row of the execution_table.
What is the base case and why is it important?
The base case stops recursion by returning a value without calling itself again. Without it, recursion never ends and causes stack overflow. In the table, step 100000 shows the base case n=0.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the stack size when n equals 99998?
A99999
B3
C99998
D100000
💡 Hint
Check the row where Call Depth (n) is 99998 and see the Stack Size column.
At which step does the base case occur in the execution table?
AStep 99999
BStep 1
CStep 100000
DStack limit reached
💡 Hint
Look for the step where n equals 0 and the function returns 0.
If the base case was missing, what would happen to the stack size?
AIt would keep increasing until stack overflow
BIt would stay the same
CIt would decrease over time
DIt would immediately cause an error
💡 Hint
Refer to the concept flow and execution_table showing stack size growing with each recursive call.
Concept Snapshot
Stack overflow happens when too many function calls fill the call stack.
Each call adds a frame to the stack.
Recursive functions must have a base case to stop.
Without stopping, stack size grows until memory runs out.
This causes a stack overflow error and crashes the program.
Full Transcript
Stack overflow occurs when a program uses too much call stack memory by calling functions repeatedly without stopping. Each function call adds a frame to the stack to remember where to return and local variables. In recursion, if the base case is missing or too deep, the stack grows until it reaches its limit. Then the program throws a stack overflow error and crashes. The example code calls recurse with a large number, causing many recursive calls. The execution table shows stack size growing with each call until the base case is reached or the stack overflows. Understanding stack overflow helps write safe recursive functions with proper base cases.