call stack
A call stack is the structure a JavaScript engine uses to track which function is running and where each caller resumes once that function finishes.
Every function call pushes a new frame onto the top of the stack. A frame holds that call’s local variables and its place in the code. When the function returns or throws, its frame comes off, and control goes back to the caller in the frame below. ECMA-262 calls this the execution context stack, and the running code always belongs to the context on top.
Each JavaScript thread, such as a page’s main thread or a worker, has one call stack, so only the function on top makes progress. The event loop starts a queued callback only after the stack is empty. Most engines also give each Error a stack trace, a snapshot of the call stack taken when the error was created.
The stack also has a limited size. ECMA-262 doesn’t set one, so the maximum depth depends on the engine and host. When recursion runs too deep, V8 in Chrome and Node.js throws a RangeError, and Firefox throws an InternalError instead.
Example
Say a receipt printer calls a price formatter in the middle of building a line. Logging on the way in and out shows each call waiting for the one above it:
> function formatPrice(cents) {
… console.log("formatPrice() starts");
… const text = `$${(cents / 100).toFixed(2)}`;
… console.log("formatPrice() returns");
… return text;
… }
undefined
> function printReceipt(cents) {
… console.log("printReceipt() starts");
… const line = `Total: ${formatPrice(cents)}`;
… console.log("printReceipt() resumes");
… return line;
… }
undefined
> printReceipt(4250)
printReceipt() starts
formatPrice() starts
formatPrice() returns
printReceipt() resumes
'Total: $42.50'
The printReceipt() frame stays on the stack, paused, while formatPrice() runs on top of it. Only after formatPrice() returns and its frame comes off does printReceipt() continue from the same spot.
The stepper below runs the same call line by line and shows each frame going on and coming off the stack:
Recursion pushes one frame per call, so deep data can fill the stack. Here, a function counts the replies in a comment thread nested 100,000 levels deep:
> function countReplies(comment) {
… return comment.reply ? 1 + countReplies(comment.reply) : 0;
… }
undefined
> const thread = Array.from({ length: 100_000 }).reduce(
… (reply) => ({ reply }),
… {},
… );
undefined
> countReplies(thread)
Uncaught RangeError: Maximum call stack size exceeded
at countReplies (REPL3:2:18)
at countReplies (REPL3:2:30)
at countReplies (REPL3:2:30)
at countReplies (REPL3:2:30)
at countReplies (REPL3:2:30)
at countReplies (REPL3:2:30)
at countReplies (REPL3:2:30)
at countReplies (REPL3:2:30)
at countReplies (REPL3:2:30)
No countReplies() call can return until the call it made returns, so the frames pile up and the engine runs out of room long before the 100,000th level. The error’s stack trace lists the top frames, all of them countReplies() calls.
Related Resources
Course
JavaScript Coding Interview: Linked Lists and Trees
Practice linked lists, recursion, and trees in JavaScript with original interview problems, an AI-code review, a pattern quiz, and a mock interview.
For additional information on related topics, take a look at the following resources:
- Functions in JavaScript (Course)
- JavaScript Errors and Debugging for Pythonistas (Course)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 16, 2026