The call stack is a list that keeps track of all the functions that are currently running. It works like a stack of plates: the last function added is the first one to finish.
Click to reveal answer
beginner
What happens when a function is called in JavaScript?
When a function is called, it is added (pushed) to the top of the call stack. The program runs the function, and when it finishes, the function is removed (popped) from the stack.
Click to reveal answer
intermediate
What is a stack overflow error?
A stack overflow happens when too many functions are added to the call stack without finishing, usually because of infinite recursion. The stack runs out of space and the program crashes.
Click to reveal answer
intermediate
How does the call stack handle nested function calls?
When a function calls another function, the new function is added on top of the stack. The program finishes the new function first, then returns to the previous one.
Click to reveal answer
intermediate
Explain the order of execution using the call stack with this code:<br><pre>function a() { b(); } function b() { c(); } function c() { console.log('Done'); } a();</pre>
The call stack adds 'a' first, then 'b' when 'a' calls it, then 'c' when 'b' calls it. 'c' runs and logs 'Done', then 'c' is removed, then 'b' finishes and is removed, then 'a' finishes and is removed.
Click to reveal answer
What does the call stack do when a function finishes running?
AIt adds the function to the bottom of the stack
BIt duplicates the function on the stack
CIt pauses the function
DIt removes the function from the top of the stack
✗ Incorrect
When a function finishes, it is removed (popped) from the top of the call stack.
What causes a stack overflow error in JavaScript?
AUsing too many variables inside a function
BToo many functions added without finishing, often due to infinite recursion
CCalling a function with wrong arguments
DRunning asynchronous code
✗ Incorrect
A stack overflow happens when the call stack gets too big, usually from infinite recursion.
If function A calls function B, what happens to the call stack?
ABoth functions run at the same time
BFunction A is removed before function B runs
CFunction B is added on top of function A
DFunction B replaces function A in the stack
✗ Incorrect
Function B is added on top of the stack, so it runs first before returning to function A.
Which function runs first in this code?
function x() { y(); } function y() { console.log('Hi'); } x();
Ax
By
Cconsole.log
DNone
✗ Incorrect
Function x runs first, then it calls y, which runs next.
What data structure best describes the call stack?
AStack (Last In, First Out)
BQueue (First In, First Out)
CArray (Random Access)
DLinked List
✗ Incorrect
The call stack works like a stack: the last function added is the first to finish.
Describe how the call stack manages function calls and returns in JavaScript.
Think about how plates stack and unstack.
You got /4 concepts.
Explain what happens when a stack overflow error occurs and why it happens.
Imagine stacking plates endlessly without removing any.
You got /4 concepts.
Practice
(1/5)
1. What does the call stack do in JavaScript?
easy
A. Keeps track of function calls in the order they are called
B. Stores variables and their values
C. Manages asynchronous events
D. Handles network requests
Solution
Step 1: Understand the role of the call stack
The call stack tracks which functions are currently running and in what order.
Step 2: Differentiate from other JavaScript components
Variables are stored in memory, asynchronous events are handled by the event loop, and network requests are managed separately.
Final Answer:
Keeps track of function calls in the order they are called -> Option A
Quick Check:
Call stack = function order tracking [OK]
Hint: Call stack = function call order tracker [OK]
Common Mistakes:
Confusing call stack with event loop
Thinking call stack stores variables
Mixing call stack with network handling
2. Which of the following is the correct way to call a function named foo in JavaScript?
easy
A. call foo();
B. function foo();
C. execute foo();
D. foo();
Solution
Step 1: Recall JavaScript function call syntax
Functions are called by writing their name followed by parentheses, like foo();.
Step 2: Identify incorrect options
"call foo();" and "execute foo();" are not valid JavaScript syntax. "function foo();" is a function declaration, not a call.
Final Answer:
foo(); -> Option D
Quick Check:
Function call = name + () [OK]
Hint: Call functions with name and parentheses () [OK]
Common Mistakes:
Using 'call' keyword incorrectly
Confusing declaration with call
Adding extra keywords like 'execute'
3. What will be the output of the following code?
function first() {
console.log('first');
second();
console.log('first again');
}
function second() {
console.log('second');
}
first();
medium
A. second\nfirst\nfirst again
B. first\nsecond\nfirst again
C. first\nfirst again\nsecond
D. first again\nsecond\nfirst
Solution
Step 1: Trace the call stack execution order
Calling first() prints 'first', then calls second() which prints 'second'. After second() finishes, control returns to first() and prints 'first again'.
Step 2: Confirm output sequence
The output lines are: 'first', 'second', 'first again' in that order.
Final Answer:
first
second
first again -> Option B
Quick Check:
Call stack order = first, second, first again [OK]
Hint: Follow function calls step-by-step [OK]
Common Mistakes:
Assuming all logs run before second()
Mixing order of console logs
Ignoring return to first() after second()
4. Identify the error in the following code related to call stack behavior:
function a() {
b();
}
function b() {
a();
}
a();
medium
A. Function b is not defined
B. Syntax error due to missing semicolons
C. Infinite recursion causing stack overflow
D. No error, code runs fine
Solution
Step 1: Analyze function calls and recursion
Function a() calls b(), and b() calls a() again, creating an endless loop.
Step 2: Understand call stack impact
This infinite mutual recursion causes the call stack to grow until it overflows, leading to a runtime error.
Final Answer:
Infinite recursion causing stack overflow -> Option C
Quick Check:
Mutual recursion without stop = stack overflow [OK]
Hint: Watch for functions calling each other endlessly [OK]
Common Mistakes:
Thinking missing semicolons cause error
Assuming functions are undefined
Believing code runs without error
5. Consider this code:
function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(3));
How many times is the factorial function added to the call stack during factorial(3) execution?
hard
A. 4 times
B. 6 times
C. 1 time
D. 3 times
Solution
Step 1: Trace recursive calls for factorial(3)
Calls happen for n=3, n=2, n=1, and n=0. Each call adds a new frame to the call stack.
Step 2: Count total calls added to the stack
There are 4 calls total before reaching the base case (n=0), so 4 stack frames are added.
Final Answer:
4 times -> Option A
Quick Check:
Recursive calls = n+1 stack frames [OK]
Hint: Count calls including base case for recursion [OK]
Common Mistakes:
Counting only n times, missing base case call
Confusing total calls with return steps
Assuming single call for recursion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.