Sub topic 5
Debugging JavaScript with breakpoints
This chapter assumes you know basic JavaScript syntax, can open DevTools, and have used the Console. It covers the Sources panel in Chromium-based browsers. Firefox calls it the Debugger and Safari calls it Sources, and both work in a similar way.
Why breakpoints beat logging
console.log shows you values you chose to print, at the moment you chose to print them. A breakpoint pauses the page at a line of code. While it is paused, you can read every variable in scope, see how execution got there, and run the code one step at a time. You do not need to guess in advance which values matter.
Finding your code in the Sources panel
Open DevTools and choose the Sources panel. It has three main areas:
- The file navigator on the left lists the page's scripts. Press Ctrl+P (Cmd+P on macOS) to open a file by name.
- The editor in the middle shows the selected file. Click a line number to set a breakpoint on that line.
- The debugger sidebar on the right holds the Watch, Breakpoints, Scope and Call Stack sections and the stepping controls.
If your site is bundled or minified, DevTools can show the original files when the build produces source maps. A source map is a file that maps the generated code back to your original source. Without one, you will step through unreadable output.
Ways to pause execution
You can pause the page in several ways:
- Line breakpoint: click a line number in the editor. Execution pauses before that line runs.
- Conditional breakpoint: right-click a line number, choose to add a conditional breakpoint, and enter an expression such as
price > 100. The page pauses only when the expression is true. This helps inside loops that run thousands of times. debuggerstatement: writedebugger;in your code. It pauses only while DevTools is open.- Event listener breakpoint: in the sidebar, expand Event Listener Breakpoints and tick an event such as
click. The page pauses when any handler for that event starts. - Pause on exceptions: the pause-on-exceptions button stops execution where an error is thrown.
Here is a function with a debugger statement:
function applyDiscount(price, percent) {
const discount = price * percent / 100;
debugger; // execution pauses here when DevTools is open
return price - discount;
}
applyDiscount(80, 25);When the page pauses, the Scope section shows price as 80, percent as 25 and discount as 20. The return line has not run yet.
Stepping through code
Once the page is paused, use the toolbar buttons or their keyboard shortcuts:
| Control | Shortcut | What it does |
|---|---|---|
| Resume | F8 | Runs until the next breakpoint |
| Step over | F10 | Runs the current line and stops at the next one, without entering called functions |
| Step into | F11 | Enters the function called on the current line |
| Step out | Shift+F11 | Finishes the current function and stops in its caller |
Use step over for lines you trust. Use step into when the bug may be inside the function being called.
Reading the paused state
While paused, three sections of the sidebar do most of the work.
Scope lists variables grouped as Local, Closure and Global. Values appear next to their names. You can double-click a value to edit it and then resume, which lets you test a fix without changing the source.
Call Stack lists the chain of function calls that led to the current line, with the most recent call at the top. Click an earlier frame to see the code and variables as they were in that caller. This answers the question "who called this with a bad value?"
Watch holds expressions that DevTools re-evaluates at every pause, such as items.length or user && user.name. Hovering over a variable in the editor also shows its current value.
Common mistakes
- Breakpoint never hits. The code may not run, the file may have changed since the page loaded, or you set the breakpoint in generated code that differs from what the browser runs. Reload the page with DevTools open and confirm the line is reached.
- Stepping into library code. Add third-party scripts to the ignore list, which tells the debugger to skip them while stepping. Right-click the file in the navigator to do this.
- Forgotten
debuggerstatements. They pause every user who opens DevTools. Remove them before you deploy. - Values that change after the pause. The Scope section shows values at the moment of the pause. Asynchronous code, such as a
fetchcallback, may run later with different state. Set a breakpoint inside the callback itself. - Pausing too often. Too many breakpoints make it hard to see the one that matters. Use the Breakpoints section to disable or remove those you no longer need.
Exercise
A function called calculateTotal is called 5,000 times in a loop, and you only care about the call where item.quantity is 0. How do you pause only on that call without editing the code, and which sidebar section then shows who called the function?
Show answer
Right-click the line number inside calculateTotal and add a conditional breakpoint with the expression item.quantity === 0. The page pauses only when that expression is true. Once paused, the Call Stack section shows the chain of calls that led to this one.
Exercise
You are paused on a line that calls formatPrice(total). You suspect the bug is inside formatPrice. Which stepping control do you use, and which would you use to leave that function once you have seen enough?
Show answer
Use Step into (F11) to enter formatPrice. Use Step out (Shift+F11) to finish it and return to the calling function.