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.
function total(prices: number[]): number { debugger; // pauses here when an inspector is attached return prices.reduce((sum, p) => sum + p, 0); }
Try it
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.
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
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-debuggerso it fails review. - Node. Run
node --inspect-brk main.js(ornode inspect) and attach DevTools or an editor; the statement then pauses like a breakpoint. TypeScript 7 output needssourceMap(and--enable-source-mapsfor stack traces) to map back to.tslines. - 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
removeCommentsornoEmitdo not change it. - Not a reserved name for variables.
debuggeris a reserved word, so you cannot use it as an identifier.