category | created | tags | title |
---|---|---|---|
Trick |
2021-03-01 |
DevTools |
Log a variable to the console using conditional breakpoints |
Sometimes it's not possible for us to log data to the Console directly in the code. For example, when we debug codes coming from external libraries.
Perform the steps below to print the variable:
- Open the Chrome DevTools, and activate the Sources tab
- Navigate to the line of a given function that you want to log the data
- Right-click the line number, and choose Add conditional breakpoint ... from the context menu
- Enter the condition
console.log('replace-it-with-your-variable');
When the associcate line is hit by the debug process, it will try to evaluate the condition meaning that the variable is printed out.
The nice thing is that the script execution on the page is still being processed because the condition never happens (the console.log
function returns undefined
).
In the following screenshot, the params
variable is logged to the Console when the sum
function is invoked.