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.
Join the DZone community and get the full member experience.
Join For FreeIn 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:
- console.log() Statement
- Setting breakpoint using developer tools available in the browser (preferred)
- Debugger Statement
- alert statement (mostly beginners will use this)
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.
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.
Opinions expressed by DZone contributors are their own.
Comments