Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

The debugger statement

Reference for the debugger statement in TypeScript: how it pauses execution, source maps, Node inspect, removeComments and conditional breakpoints.

debugger; is a statement that asks the JavaScript engine to pause, as if a breakpoint were set on that line. If no debugger is attached it does nothing at all. When DevTools or an inspector is attached, execution stops there and you can look at variables, the call stack and the scope chain.

TypeScript passes the statement through unchanged. With source maps enabled, the debugger shows your .ts file and stops on the matching line, so you debug the code you wrote instead of the emitted JavaScript.

TypeScript
function total(prices: number[]): number {
  debugger; // pauses here when an inspector is attached
  return prices.reduce((sum, p) => sum + p, 0);
}

Try it

TypeScript debugger is a no-op without an inspector

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Pausing only when a condition holds

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
debugger;

The statement has no operands and no value. It is allowed anywhere a statement is allowed (not inside an expression), and the semicolon follows the usual automatic-insertion rules.

Usage notes

  • Zero effect without a debugger. The engine ignores it, which is why it is easy to forget in production code. Add the lint rule no-debugger so it fails review.
  • Node. Run node --inspect-brk main.js (or node inspect) and attach DevTools or an editor; the statement then pauses like a breakpoint. TypeScript 7 output needs sourceMap (and --enable-source-maps for stack traces) to map back to .ts lines.
  • Browsers. DevTools must be open for the pause to happen. Bundlers can strip it: esbuild has a drop: ["debugger"] option and Terser removes it by default when configured to drop debugger statements.
  • Compared with console.log. A pause lets you inspect everything in scope and step, while logging is better for long-running or asynchronous flows you cannot stop.
  • Conditional breakpoints in the editor are cleaner than if (...) { debugger; }, because they leave no code behind; the statement form is useful when the condition needs program logic.
  • Does not affect types. The compiler does not analyze it, and removeComments or noEmit do not change it.
  • Not a reserved name for variables. debugger is a reserved word, so you cannot use it as an identifier.