How to set a JavaScript breakpoint from code in Chrome?

JavascriptDebuggingGoogle ChromeBreakpoints

Javascript Problem Overview


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().

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

Command Line API Reference: debug

Solution 3 - Javascript

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

Solution 4 - Javascript

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/

Solution 5 - Javascript

On the "Scripts" tab, go to where your code is. At the left of the line number, click. This will set a breakpoint.

Screenshot:

screenshot of breakpoint in chrome

You will then be able to track your breakpoints within the right tab (as shown in the screenshot).

Solution 6 - Javascript

debugger is a reserved keyword by EcmaScript and given optional semantics since ES5

As a result, it can be used not only in Chrome, but also Firefox and Node.js via node debug myscript.js.

The standard says:

> Syntax

> DebuggerStatement : > debugger ;

> Semantics

> Evaluating the DebuggerStatement production may allow an implementation to cause a breakpoint when run under a debugger. If a debugger is not present or active this statement has no observable effect.

> The production DebuggerStatement : debugger ; is evaluated as follows:

> 1. If an implementation defined debugging facility is available and enabled, then 1. Perform an implementation defined debugging action. 1. Let result be an implementation defined Completion value.

  1. Else
    1. Let result be (normal, empty, empty).
  2. Return result.

No changes in ES6.

Solution 7 - Javascript

There are many ways to debug JavaScript code. Following two approaches are widely used to debug JavaScript via code

  1. Using console.log() to print out the values in the browser console. (This will help you understand the values at certain points of your code)

  2. Debugger keyword. Add debugger; to the locations you want to debug, and open the browser's developer console and navigate to the sources tab.

For more tools and ways in which you debug JavaScript Code, are given in this link by W3School.

Solution 8 - Javascript

It is possible and there are many reasons you might want to do this. For example debugging a javascript infinite loop close to the start of the page loading, that stops the chrome developer toolset (or firebug) from loading correctly.

See section 2 of

http://www.laurencegellert.com/2012/05/the-three-ways-of-setting-breakpoints-in-javascript/

or just add a line containing the word debugger to your code at the required test point.

Solution 9 - Javascript

> Breakpoint :- > > breakpoint will stop executing, and let you examine JavaScript values. > > After examining values, you can resume the execution of code (typically with a play button). > > Debugger :- > >The debugger; stops the execution of JavaScript, and callsthe debugging function. > >The debugger statement suspends execution, but it does not close any files or clear any variables.

Example:-
function checkBuggyStuff() {
  debugger; // do buggy stuff to examine.
};

Solution 10 - Javascript

You can set debug(functionName) to debug functions as well.

https://developers.google.com/web/tools/chrome-devtools/javascript/breakpoints#function

Solution 11 - Javascript

I wouldn't recommend debugger; if you just want to kill and stop the javascript code, since debugger; will just temporally freeze your javascript code and not stop it permanently.

If you want to properly kill and stop javascript code at your command use the following:

throw new Error("This error message appears because I placed it");

Solution 12 - Javascript

This gist Git pre-commit hook to remove stray debugger statements from your merb project

maybe useful if want to remove debugger breakpoints while commit

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaulView Question on Stackoverflow
Solution 1 - Javascriptxn.View Answer on Stackoverflow
Solution 2 - JavascriptProtector oneView Answer on Stackoverflow
Solution 3 - JavascriptmartynasView Answer on Stackoverflow
Solution 4 - JavascriptAndrijaView Answer on Stackoverflow
Solution 5 - JavascriptFlorian MargaineView Answer on Stackoverflow
Solution 6 - JavascriptCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 7 - JavascriptKeet SugathadasaView Answer on Stackoverflow
Solution 8 - Javascriptuser2539341View Answer on Stackoverflow
Solution 9 - JavascriptParth RavalView Answer on Stackoverflow
Solution 10 - JavascriptLanceView Answer on Stackoverflow
Solution 11 - JavascriptS ToView Answer on Stackoverflow
Solution 12 - JavascripttowithView Answer on Stackoverflow