JavaScript Reference ยท Statements and Declarations
The debugger statement
Reference for the JavaScript debugger statement: how it pauses execution when developer tools are open, conditional breakpoints in code, and when to remove it.
The debugger statement acts as a breakpoint written into your code. When a debugger is attached (for example, the browser's developer tools are open, or Node.js was started with --inspect), execution pauses at that line and you can inspect variables, the call stack and scopes, then step forward.
When no debugger is attached, the statement does nothing.
function total(items) { debugger; return items.reduce((sum, i) => sum + i.price, 0); }
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
debugger;
It takes no arguments and produces no value.
Usage notes
- Needs developer tools. In a browser, the statement pauses only while the developer tools are open. In Node.js, run with
node --inspect-brk file.jsor attach an inspector. - Conditional breakpoints. Wrapping
debuggerin anifgives a breakpoint that triggers only for the interesting case. Developer tools also offer conditional breakpoints without editing code. - Do not ship it. Remove
debuggerbefore releasing. Linters flag it (for example with ano-debuggerrule), and leaving it in annoys users who open developer tools. - Not logging.
console.log()prints values;debuggerlets you explore everything in scope interactively. - Strict mode does not change its behavior.
- Source maps. With bundled code, the debugger pauses in the original source if source maps are loaded.
- Sandboxed iframes such as the live editors on this site run the code but cannot show a debugger for you unless you open the developer tools for that frame.
Browser support
debugger is part of the language and every browser with developer tools honors it.
Related
consolemethods,throwtry...catch...finally- Lesson: Debugging, Console and debugging