Using Conditional Breakpoints and Logpoints in Chromium
This article provides a straightforward guide on how web developers can optimize their debugging workflow using conditional breakpoints and logpoints within the Sources panel of Chromium-based browsers. It covers how to access the Sources panel, set up conditional breakpoints that only pause execution when specific criteria are met, and utilize logpoints to output messages to the console without cluttering the actual codebase with temporary logging statements.
Accessing the Sources Panel
Before setting any breakpoints, you must locate the file you want to debug in the Chromium Developer Tools.
- Open the Chromium browser and navigate to your web application.
- Open Developer Tools by pressing
F12, orCtrl+Shift+Ion Windows/Linux, orCmd+Option+Ion macOS. - Click on the Sources tab at the top of the Developer Tools window.
- Use the Page pane on the left side to navigate the
file tree and select the JavaScript file you need to debug.
(Alternatively, press
Ctrl+PorCmd+Pto quickly search for and open a file by name).
How to Use Conditional Breakpoints
A standard breakpoint pauses code execution every time a specific
line is reached. A conditional breakpoint only pauses execution when a
custom JavaScript expression you provide evaluates to true.
This is ideal for debugging loops or frequently called functions.
To set a conditional breakpoint:
- In the code editor pane of the Sources panel, find the line of code where you want to pause execution.
- Right-click on the line number in the left-hand gutter (the column showing the line numbers).
- Select Add conditional breakpoint... from the context menu.
- An inline text box will appear below the line of code.
- Type your condition into the box. This must be a valid JavaScript
expression that uses variables in the current scope (for example,
i === 50oruser.isAuthenticated === false). - Press
Enterto save and activate the breakpoint.
An orange icon with a question mark will appear over the line number. Now, when your application runs, the debugger will ignore this line unless your specified condition is met.
How to Use Logpoints
Developers often add console.log() statements directly
into their source code to inspect variables, which requires saving the
file, reloading the page, and remembering to delete the logs later.
Logpoints solve this by allowing you to inject console logs directly
through the browser without modifying your code or pausing
execution.
To set a logpoint:
- Locate the line of code where you want to log information.
- Right-click the line number in the gutter.
- Select Add logpoint... from the context menu.
- An inline text box will appear, similar to the conditional breakpoint input.
- Enter the expression you want to log. You do not need to type
console.log(). Simply type the exact arguments you would pass into a console log. You can combine strings and variables separated by commas (for example,'Current user ID:', user.id, 'Status:', status). - Press
Enterto save and activate the logpoint.
A pink icon with two dots will appear over the line number. Every time the browser executes that line of code, it will output your specified message to the Console panel without interrupting the application's execution flow.
Managing Breakpoints and Logpoints
Once you have set conditional breakpoints and logpoints, you can manage them using the Breakpoints pane, located on the right-hand side of the Sources panel (or at the bottom, depending on your layout).
- Edit: Right-click the icon in the line number gutter and select Edit condition... or Edit logpoint... to change the expression.
- Disable temporarily: Uncheck the box next to the
specific breakpoint or logpoint in the Breakpoints
pane, or hold
Shiftand click the icon in the gutter. - Delete: Left-click the icon in the gutter, or right-click it and select Remove breakpoint. You can also right-click inside the Breakpoints pane and select Remove all breakpoints to clear everything at once.