Conditional Breakpoints in VSCode
Imagine you need to debug a loop with numerous iterations and you wish to halt the execution only when a specific condition is met. A conditional breakpoint is your solution. This tool is also handy when you want to display the value of a variable in the console under certain conditions.
In this post, I'll guide you through using conditional breakpoints in VSCode.
How to Set a Conditional Breakpoint
To set a conditional breakpoint, right-click on the line number where you want to set it and select Add Conditional Breakpoint...
There are three options available:
Hit count
This option allows VSCode to pause when the breakpoint has been triggered a specified number of times. For instance, to halt execution on the third hit, enter 3 in the input field.
Log message
If you prefer to print a message in the console each time the breakpoint is hit, choose this option. To display Breakpoint hit! in the console upon each trigger, simply set this as your input value. You also can print the value of a variable.
Expression
My personal favorite is the 'Expression' option. Here, you can define a specific condition to pause execution. It's particularly powerful as you can combine multiple conditions using logical operators like &&
, ||
, !
, ==
, >=
, <=
, >
, <
, etc.