0
0
Javascriptprogramming~5 mins

Call stack behavior in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the call stack in JavaScript?
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
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
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
Which function runs first in this code?<br>
function x() { y(); } function y() { console.log('Hi'); } x();
Ax
By
Cconsole.log
DNone
What data structure best describes the call stack?
AStack (Last In, First Out)
BQueue (First In, First Out)
CArray (Random Access)
DLinked List
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.