liveBook · Manning

Stack trace

As explained in JavaScript in Depth

A report of active call frames that records the sequence of function calls leading to a point in execution, typically for diagnostics.

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

How JavaScript in Depth covers it

How deep it goes
Explains call-chain capture, structured frames, lazy serialization, and memory cost, but leaves formatting engine-specific.
Examples
Uses nested inner, middle, and outer functions to show frames, file locations, and console output.
What it assumes
Assumes familiarity with JavaScript functions and Error objects while establishing stack traces as runtime-provided debugging behavior.
What you can do after
Diagnose failures by tracing the recorded call chain and distinguish debugging details from recovery data.
How it connects
Connects stack traces with Error objects, call stacks, error handling, recovery, and asynchronous operations.

Also explained in

Compare how 9 books explain stack trace →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.