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
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
In the following screenshot, the
params variable is logged to the Console when the
sum function is invoked.