Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 40 ยท Debugging

Breakpoint Types, Watch and the Debug Console

Go beyond simple breakpoints in VS Code: conditional breakpoints, hit counts, logpoints, exception breakpoints, Watch expressions and the Debug Console, with a runnable condition model.

A plain breakpoint stops every time the line runs. In a loop that runs ten thousand times, that is useless. VS Code offers smarter breakpoints that stop only when something interesting happens, plus tools to watch values change. This lesson covers each type with examples.

Editing a breakpoint

Right-click in the gutter on a line (or right-click an existing red dot) and choose Add Conditional Breakpoint..., Add Logpoint... or Add Triggered Breakpoint.... Existing breakpoints can be edited with Edit Breakpoint.... The red dot changes shape to show the type: a dot with an equals sign for a condition, a diamond for a logpoint, and gray for disabled.

The Breakpoints section of the Run and Debug view lists them all, with checkboxes to enable or disable each without deleting it.

Conditional breakpoints

Choose Expression and type a boolean expression in the language you are debugging. The program stops only when it is true.

JavaScript
for (let i = 0; i < 10000; i++) {
  const price = prices[i];          // condition: i === 7342
  total += price;                   // condition: price < 0
}

Examples of useful conditions:

  • i === 7342 stop at one iteration
  • user.id === 42 stop for one particular record
  • name.includes("test") stop for matching strings
  • total > 1000 && items.length === 0 combine several checks

If the expression has an error, the debugger stops anyway and shows the problem, so check the syntax.

Hit count breakpoints

Choose Hit Count and enter a number or an operator and number: > 100, == 5, % 10. The breakpoint stops when the line has been reached that many times, for example % 100 stops on every hundredth pass. Use it when you know the failure happens "after a while".

Logpoints: print without editing code

A logpoint prints a message to the Debug Console when the line is hit and does not stop the program. You write the message with curly braces around expressions:

TEXT
Processing order {order.id} with total {total}

This is console.log that you never have to delete, and cannot accidentally commit. Logpoints are great for timing-sensitive code (stopping would change the behavior) and for servers handling many requests.

Function breakpoints and data breakpoints

  • Function breakpoint: in the Breakpoints view click + and type a function name. The program stops when it is called. Useful when you do not know where a function is defined or called from. Supported by many, not all, debug adapters.
  • Data breakpoint (watchpoint): in the Variables view, right-click a variable and choose Break on Value Change (or on access). The program stops whenever the value changes, no matter which line did it. Support is language dependent; it is common in C and C++ debuggers and in .NET, less so in JavaScript.
  • Triggered breakpoint: stays inactive until another chosen breakpoint is hit. This lets you skip startup noise: only start stopping after a specific point.

Exception breakpoints

In the Breakpoints view, check boxes such as Uncaught Exceptions and Caught Exceptions (for Node.js; Python offers Raised Exceptions, Uncaught Exceptions and User Uncaught Exceptions). The debugger pauses at the line that throws, with the full state visible, instead of after the program has crashed. Turn on Uncaught Exceptions while hunting a crash; turn on Caught carefully, because libraries throw and catch exceptions routinely.

Watch expressions

The Watch section keeps expressions evaluated every time the program pauses. Click + and type cart.items.length, total / count, or JSON.stringify(user). Values update with each step. Red values with an error such as not available mean the variable is out of scope at the current line.

You can also right-click a variable > Add to Watch. Watch expressions are re-evaluated, so avoid expressions with side effects (like i++) that would change the program state. Also avoid expensive ones when stepping through hot code.

The Debug Console

The Debug Console (Ctrl+Shift+Y) is a REPL attached to the paused program:

  • Type an expression, e.g. numbers.filter(n => n > 10), and it evaluates in the current frame, with access to the local variables.
  • Assignments change real state: total = 0 really resets the variable.
  • Output from logpoints and program output (for adapters that capture it) appears here.
  • Use the up arrow for history.

Programs launched with "console": "integratedTerminal" print to the terminal instead, which is better when the program reads input. See launch.json.

Variable inspection tips

  • Hover shows a value; expand objects with the arrow.
  • Long strings and big arrays are truncated; use the Debug Console (JSON.stringify(x, null, 2)) or Copy Value.
  • In Python and JS you can Set Value by double-clicking.
  • Inline values and inline breakpoints (Shift+F9, a breakpoint on one statement of a multi-statement line) give finer control.

A stand-in: conditions and hit counts

This JavaScript stand-in runs a loop and counts how often a breakpoint with a condition, hit count and logpoint would fire. It illustrates the three different ideas on the same loop, without a real debugger.

JS Stand-in: which breakpoints would stop?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In a real session each "would pause" is a stop with the Call Stack and Variables available. The logpoint line shows output only.

JS Stand-in: a watch list over several steps

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Habits for effective debugging

  1. Prefer logpoints to temporary console.log lines.
  2. Use conditions when the loop is long; do not press F5 a thousand times.
  3. Catch the exception at the throw, not after it unwinds.
  4. Watch small expressions that answer a question ("is this null?").
  5. Disable, do not delete breakpoints you may need again.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a logpoint?

    Logpoints write a message to the Debug Console without pausing execution and without changing your code.
  2. 2. Which breakpoint type helps stop only on the 100th pass of a loop?

    A hit count such as == 100 stops when the line has been reached that many times.
  3. 3. Why enable Uncaught Exceptions breakpoints?

    The debugger pauses where the error is thrown so you can inspect variables.
  4. 4. What can you type into the Debug Console while paused?

    The Debug Console evaluates expressions in the paused context, with access to local variables.

Summary

  • Conditional and hit-count breakpoints stop only when a situation matters.
  • Logpoints print messages without stopping and without changing source code.
  • Exception breakpoints pause where errors are thrown; data breakpoints watch value changes.
  • Watch expressions are re-evaluated at every pause; the Debug Console evaluates ad hoc expressions.
  • Disable rather than delete breakpoints you might need again.

Next: launch.json: Debug Configurations