Browser developer tools give you a console, a step-through debugger, and a network monitor for diagnosing exactly what a script is doing at runtime. This lesson focuses on the debugger; statement, which pauses code execution wherever it's placed so you can inspect variables and step through logic line by line.
1Debugging Tools in JavaScript | Web Dev - In-Depth Guide Part 1
Browser dev tools provide a console, debugger, and network monitor. Use 'debugger;' to pause execution and inspect state.
const bug = (val) => {
debugger;
return val * 2;
};Debugging Tools
2Step-by-Step Breakdown
Browser dev tools provide a console, debugger, and network monitor. Use 'debugger;' to pause execution and inspect state.
Level Up š
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Use the Accessibility Tree Inspector Alongside the Console
Chrome and Firefox dev tools include an Accessibility pane that shows the accessibility tree, computed ARIA roles, and name/description for any element ā use it the same way you'd use the console to debug logic, but for debugging what screen readers actually perceive.
// DevTools > Elements > Accessibility paneSEO Implications
- 1
Leftover debugger; Statements Can Freeze a Page for Users on Certain Setups
If a debugger; statement accidentally ships to production and a user has their browser's dev tools open, execution pauses indefinitely ā for users with accessibility extensions or automated crawlers that open dev tools/inspect the page, this can result in a hung, unresponsive page that never finishes rendering content.
Best Practices
Remove All debugger; Statements Before Committing or Deploying
A debugger; statement left in shipped code has no effect for users without dev tools open, but for users or automated tools that do have them open, it silently pauses execution ā treat it exactly like a console.log() you'd never want to ship, and configure a linter rule to catch it.
Use Conditional Breakpoints Instead of Wrapping debugger; in an if
Rather than writing `if (i === 500) { debugger; }` inside a loop, right-click the line number in the Sources panel and add a conditional breakpoint with the same condition ā it achieves the same pause without leaving debugging code mixed into your actual source.
Frequent Bugs
A debugger; statement shipped to production goes unnoticed for a long time.
Because debugger; only pauses execution when browser dev tools happen to be open, it can silently sit in shipped code for a long time before someone with dev tools open hits it and gets confused by an unexplained freeze. Add an ESLint rule like no-debugger to fail the build if one slips into a commit.
Real-World Examples
Using debugger; to Inspect a Miscalculated Total
A shopping cart's total price was coming out wrong, and console.log() statements weren't giving enough context about the full state of the cart at the moment of the bug.
function calculateTotal(items) {
let total = 0;
for (const item of items) {
debugger; // Pauses here each iteration so you can inspect `item` and `total`
total += item.price * item.quantity;
}
return total;
}