A stack overflow happens when a program uses more memory on the call stack than is available. This usually occurs due to too many nested function calls or infinite recursion.
Click to reveal answer
beginner
How does recursion relate to stack overflow?
Recursion is when a function calls itself. If the recursion never stops or stops too late, it keeps adding calls to the stack, which can cause a stack overflow.
Click to reveal answer
beginner
What is the call stack in JavaScript?
The call stack is a place where JavaScript keeps track of function calls. Each time a function runs, it is added to the stack. When it finishes, it is removed.
Click to reveal answer
intermediate
What happens when a stack overflow occurs in JavaScript?
When a stack overflow happens, JavaScript throws a 'RangeError: Maximum call stack size exceeded' error and stops running the current execution context.
Click to reveal answer
intermediate
How can you prevent stack overflow caused by recursion?
You can prevent stack overflow by making sure recursive functions have a base case that stops the recursion, or by using loops instead of recursion.
Click to reveal answer
What causes a stack overflow in JavaScript?
ARunning out of disk space
BUsing too many variables
CToo many nested function calls or infinite recursion
DSyntax errors in code
✗ Incorrect
A stack overflow happens when the call stack exceeds its limit due to too many nested calls or infinite recursion.
What error message does JavaScript show when a stack overflow occurs?
ARangeError: Maximum call stack size exceeded
BTypeError: undefined is not a function
CReferenceError: variable is not defined
DSyntaxError: Unexpected token
✗ Incorrect
JavaScript throws a RangeError with the message 'Maximum call stack size exceeded' when a stack overflow happens.
Which of these can help avoid stack overflow in recursive functions?
AAdding a base case to stop recursion
BRemoving all function calls
CUsing global variables
DCalling functions asynchronously
✗ Incorrect
A base case stops recursion from continuing forever, preventing stack overflow.
What is the call stack used for in JavaScript?
AStoring variables
BManaging network requests
CSaving files
DTracking function calls
✗ Incorrect
The call stack keeps track of which functions are running and in what order.
If a recursive function never reaches a base case, what will happen?
AThe program will run faster
BA stack overflow error will occur
CThe program will stop immediately
DThe function will return undefined
✗ Incorrect
Without a base case, recursion continues indefinitely, causing a stack overflow.
Explain what a stack overflow is and why it happens in JavaScript.
Think about how functions are added and removed from the call stack.
You got /4 concepts.
Describe how you can prevent stack overflow when writing recursive functions.
Focus on controlling when recursion ends.
You got /3 concepts.
Practice
(1/5)
1. What causes a stack overflow error in JavaScript?
easy
A. Declaring global variables
B. Using too many variables in a function
C. Too many nested function calls without stopping
D. Calling a function only once
Solution
Step 1: Understand what the call stack is
The call stack keeps track of function calls in JavaScript. Each call uses some memory.
Step 2: Identify what causes overflow
If functions call themselves too many times without stopping, the stack runs out of memory, causing overflow.
Final Answer:
Too many nested function calls without stopping -> Option C
Quick Check:
Stack overflow = too many nested calls [OK]
Hint: Stack overflow = infinite or too deep recursion [OK]
Common Mistakes:
Thinking global variables cause stack overflow
Confusing variable count with call stack size
Assuming single function call causes overflow
2. Which of the following JavaScript function definitions will cause a stack overflow when called?
easy
A. function infinite() { infinite(); }
B. function countDown(n) { if(n <= 0) return; countDown(n - 1); }
C. function greet() { console.log('Hello'); }
D. function add(a, b) { return a + b; }
Solution
Step 1: Check each function for recursion
function countDown(n) { if(n <= 0) return; countDown(n - 1); } has a base case stopping recursion. function infinite() { infinite(); } calls itself endlessly without a base case. Options C and D are normal functions without recursion.
Step 2: Identify which causes stack overflow
Only function infinite() { infinite(); } causes infinite recursion, leading to stack overflow error.
Final Answer:
function infinite() { infinite(); } -> Option A
Quick Check:
Infinite recursion = stack overflow [OK]
Hint: Infinite recursion without base case causes overflow [OK]
Common Mistakes:
Assuming all recursive functions cause overflow
Ignoring base case in recursion
Confusing normal functions with recursive ones
3. What will be the output of this code?
function recurse(n) {
if (n === 0) return 'Done';
return recurse(n - 1);
}
console.log(recurse(3));
medium
A. 3
B. Stack overflow error
C. undefined
D. "Done"
Solution
Step 1: Trace the recursive calls
recurse(3) calls recurse(2), then recurse(1), then recurse(0). At n=0, it returns 'Done'.
Step 2: Return values propagate back
Each call returns the result of the next call, so finally recurse(3) returns 'Done'.
Final Answer:
"Done" -> Option D
Quick Check:
Base case stops recursion, returns 'Done' [OK]
Hint: Base case returns value, recursion unwinds safely [OK]
Common Mistakes:
Expecting stack overflow without base case
Thinking recursion returns input number
Ignoring return propagation
4. Find the error in this recursive function that causes a stack overflow:
function factorial(n) {
return n * factorial(n - 1);
}
medium
A. Wrong multiplication operator
B. Missing base case to stop recursion
C. Function name is incorrect
D. Should use addition instead of multiplication
Solution
Step 1: Check for base case in recursion
The function calls itself without any condition to stop, so recursion never ends.
Step 2: Understand stack overflow cause
Without a base case, calls pile up until memory runs out, causing stack overflow.
Final Answer:
Missing base case to stop recursion -> Option B
Quick Check:
Base case missing = infinite recursion [OK]
Hint: Always add base case to recursive functions [OK]
Common Mistakes:
Assuming multiplication causes error
Ignoring need for base case
Thinking function name affects recursion
5. You want to safely calculate the sum of numbers from 1 to n using recursion without causing stack overflow. Which change should you make to this function?
function sum(n) {
return n + sum(n - 1);
}
hard
A. Add a base case: if (n <= 0) return 0;
B. Change + to * operator
C. Call sum(n + 1) instead
D. Remove the recursive call
Solution
Step 1: Identify missing base case
The function calls itself with n-1 but never stops, causing overflow.
Step 2: Add base case to stop recursion
Adding if (n <= 0) return 0; stops recursion when n reaches 0 or less.
Step 3: Confirm safe recursion
With base case, recursion ends properly and sum is calculated without overflow.
Final Answer:
Add a base case: if (n <= 0) return 0; -> Option A
Quick Check:
Base case stops recursion safely [OK]
Hint: Base case prevents infinite recursion and overflow [OK]
Common Mistakes:
Changing operator instead of adding base case
Increasing n causing infinite recursion
Removing recursion loses function purpose
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.