70

I am currently debugging complex JavaScript code with Firebug. I am looking for a way to stop the JavaScript execution as if it was a breakpoint programmatically.

Example:

instructions ...
degugger.breakpoint(); // the execution stops here as if a breakpoint was
                       // manually set
other instructions ...
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177

1 Answers1

121

You can use the debugger statement:

// your JS code
...
// break here
debugger;

It works in all major browsers.

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
RaYell
  • 69,610
  • 20
  • 126
  • 152
  • 4
    It will also work with IE if you enable Script Debugging in the Internet Options. – Kirtan Aug 12 '09 at 11:44
  • 1
    After you have done this, you should be then be able to use breakpoints in your IDE debugger. This saves you from having to declare multiple 'debugger;' lines of code across your application. – James Wiseman Aug 12 '09 at 13:31
  • 2
    I'm surprised how long I've gone without realizing you could do this. Thanks, this is very helpful. I would add that, for Chromium on Linux, this line of code will only pause execution if the debugger is open (and the disable breakpoints toggle is not selected). – snapfractalpop Mar 06 '14 at 21:20
  • this didn't work for me in firefox. Do you have to have the debugger open? I had firebug open but was looking at the console + network tabs. I wasn't actually interested in the code/state at the time but just wanted the console output to stop – JonnyRaa Sep 11 '14 at 14:15
  • The debugger directive now works in Opera. (Tested in Opera 28) – James Dunn Apr 28 '15 at 20:28