logo

Call Stack

Explain the call stack, how function calls are tracked, and stack traces.

Short explanation

The call stack is where execution contexts are pushed when functions run and popped when they return. Stack traces show the chain of function calls leading to an error.

Small snippet

function a() {
  b();
}
function b() {
  c();
}
function c() {
  throw new Error('oops');
}

try {
  a();
} catch (e) {
  console.log(e.stack);
}

How JS handles it internally

Each function call creates a frame holding references to local variables, arguments, and the current instruction pointer. Deep recursion can overflow the stack leading to errors.

FAQ

Q: How do I interpret a stack trace?

A: Read from top to bottom β€” the topmost frame is where the error occurred; earlier frames show call origins.