How to set a JavaScript breakpoint from code in Chrome?

I want to force the Chrome debugger to break on a line via code, or else using some sort of comment tag such as something like console.break().


You can use debugger; within your code. If the developer console is open, execution will break. It works in firebug as well.


You can also use debug(function), to break when function is called.

Command Line API Reference: debug


Set up a button click listener and call the debugger;

Example

$("#myBtn").click(function() {
 debugger;   
});

Demo

http://jsfiddle.net/hBCH5/

Resources on debugging in JavaScript

  • http://www.laurencegellert.com/2012/05/the-three-ways-of-setting-breakpoints-in-javascript/
  • http://berzniz.com/post/78260747646/5-javascript-debugging-tips-youll-start-using-today

As other have already said, debugger; is the way to go. I wrote a small script that you can use from the command line in a browser to set and remove breakpoint right before function call: http://andrijac.github.io/blog/2014/01/31/javascript-breakpoint/