Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JavaScript: Break Your Code With the Debugger Statement

DZone's Guide to

JavaScript: Break Your Code With the Debugger Statement

Learn about using the debugger statement in JavaScript, one of the most common ways of performing client-side debugging with this library.

Free Resource

In this post, we will learn more about the debugger statement available in the JavaScript library. Many JavaScript developers may not aware of the debugger statement. I generally believe that most JavaScript developers use any of the below four methods for client-side debugging:

Here, we are primarily going to talk about the debugger statement. Before that, the console object provides access to the browser's debugging console and has many inbuilt methods available with it. Some of the example methods are

console.trace();
console.warn();
console.log();
console.info();
console.error();
console.clear();


You can use these methods to differentiate the console message. Check this URL to learn more about console objects.  

Debugger Statement

The debugger statement invokes any available debugging functionality, such as setting a breakpoint. If no debugging functionality is available, this statement has no effect.

Syntax:

 debugger; 

Code:

function potentiallyBuggyCodeAvailable(){
   code;
   debugger;
}


When the debugger is invoked, execution is paused at the debugger statement. It is like a breakpoint in the script source.

Javascript Debugger

In the above code, when the submit button is clicked it, will call sum() method  and the code will break where the debugger statement is added.

In the above screenshot, the code was broken where the debugger statement is added. It will be really helpful under certain circumstances.

Next Article : [Javascript] Exception Handling with Try...Catch Block

If you enjoyed this article, please share it with your developer friends on social media.

Topics:
javascript ,debugger ,debugging ,performance

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}