A stack trace captures the active function-call chain when an Error is created. It helps developers locate failures, while recovery logic should rely on error categories rather than source locations. section 7.4 section 7.1
Several runtime details explain both its usefulness and its cost:
- Treat the stack trace as implementation behavior: the JavaScript specification does not define it, so engines and runtimes provide it independently. section 7.4
- Understand the representation as structured frame objects containing function names and source locations; the runtime serializes them into a string only when stack is accessed. section 7.4.1
- Account for overhead even when stack is never read: every Error records debugging frames, consuming memory and CPU-related resources. section 7.4 section 7.4.1
- Keep stack traces separate from recovery decisions; line numbers and call history serve diagnosis, not retry or fallback policy. section 7.1
Following a failure through callers
If inner() throws new Error("Problem here"), the resulting trace lists inner, middle, outer, and the top-level call site with file locations. Reading upward reveals how execution reached the failure. section 7.4
function inner() {
throw new Error("Problem here");
}
function middle() {
return inner();
}
function outer() {
return middle();
}
try {
outer();
} catch (e) {
console.log(e.stack);
}Run the nested-call example and inspect the generated stack output alongside its source locations. section 7.4








