Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 40 ยท Debugging

Debugging Basics: Breakpoints and Stepping

Start debugging in VS Code: set breakpoints, run with F5, step over, into and out with F10 and F11, read the Variables and Call Stack views and trace a bug with a runnable stepping model.

Printing values with console.log or print works, but a debugger lets you pause a running program, look at every variable, and move through the code one line at a time. VS Code has a built-in debugging user interface, and extensions provide debug adapters for each language. This lesson teaches the core workflow, which is the same for JavaScript, Python, Java, C# and others.

The debugger needs a real program running on your computer, so it cannot run on this website. This lesson describes the actual interface exactly. The runnable examples are stand-ins that model how stepping and breakpoints work.

The idea

A debugger starts your program under its control. When execution reaches a breakpoint, the program pauses before running that line. While paused you can:

  • inspect and change variables,
  • see the chain of function calls that led here (the call stack),
  • run the next line, step into a function, or continue to the next breakpoint.

Everything you do is in the Run and Debug view (Ctrl+Shift+D) and the Debug toolbar.

Your first debugging session (Node.js)

Create a file sum.js:

JavaScript
function sum(numbers) {
  let total = 0;
  for (const n of numbers) {
    total += n;
  }
  return total;
}

const data = [4, 8, 15, 16, 23, 42];
const result = sum(data);
console.log("Total:", result);
  1. Open the file and click in the gutter (the margin left of the line numbers) next to total += n;. A red dot appears: that is a breakpoint. (Keyboard: put the cursor on the line and press F9.)
  2. Press F5. If asked to choose an environment, pick Node.js. (More on configuration in launch.json.)
  3. The program runs and stops at the breakpoint. The line is highlighted in yellow, and the Status Bar turns orange.

Now look around:

  • Variables (left): shows Local variables, including total, n and numbers. Expand numbers to see its elements.
  • Call Stack: shows sum called from the main script.
  • Hover over total in the editor to see its value.
  • Debug Console (bottom): type an expression such as total + n and press Enter to evaluate it in the current context.

The debug toolbar

A small floating toolbar appears at the top with these buttons:

Button Shortcut What it does
Continue F5 Run until the next breakpoint (or the end)
Step Over F10 Run the current line; do not enter functions it calls
Step Into F11 Enter the function called on this line
Step Out Shift+F11 Finish the current function and return to the caller
Restart Ctrl+Shift+F5 Stop and start again
Stop Shift+F5 End the session

Step Over versus Step Into is the key choice. If the current line is const r = sum(data);, Step Over runs the entire sum call and stops on the next line, while Step Into jumps to the first line inside sum. Use Step Over for code you trust, Step Into for code you suspect, and Step Out when you stepped into something by accident.

Run to Cursor: right-click a line and choose Run to Cursor to continue until that line without a permanent breakpoint.

A stand-in: stepping through a program

To see how these buttons behave, this JavaScript stand-in executes a tiny "program" described as a list of steps. Each step is either a plain line or a call to a function with its own steps. The model supports step over, step into and step out, and prints a trace like a debugger would. It is a teaching model, not a real debugger.

JS Stand-in: step over, into and out

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

No output.

Read the trace: after the first Step Over the debugger is on the sum(data) line, Step Into enters sum, and Step Out returns to the caller's next line. That is exactly what the real toolbar buttons do.

Reading the Variables view

  • Local shows variables of the current function; Closure and Global (in Node and browsers) show outer scopes.
  • Values changed by the last step flash briefly.
  • Double-click a value (or use Set Value from the context menu) to change it while paused; the program continues with the new value. This lets you test a fix without restarting.
  • Right-click a variable for Copy Value, Add to Watch and Copy as Expression.

The Call Stack

The stack lists functions from the current one (top) to the entry point (bottom). Click a frame to see the code and variables of that call. When an error is thrown, the stack shows the path to the failure. Frames that belong to library code can be hidden with the skipFiles option in the debug configuration (see launch.json).

Stopping on a bug: the workflow

  1. Reproduce the bug and note what you expect versus what happens.
  2. Guess where the first wrong value appears; set a breakpoint just before it.
  3. Run under the debugger and inspect variables at the breakpoint.
  4. Step forward and watch for the first value that is not what you expected.
  5. Fix the cause, restart, and confirm.

Common beginner mistake: placing the breakpoint after the line that is wrong. Start earlier and walk forward.

Other debugging controls

  • Inline values: with debug.inlineValues on, current values appear in gray next to variables in the editor.
  • Debug console accepts expressions and, for some languages, commands.
  • Breakpoints view lists all breakpoints; checkboxes enable and disable them; Remove All Breakpoints is in its toolbar.
  • Press F9 on a line with a breakpoint to remove it; disabled breakpoints show as gray circles.

Breakpoints are saved with the workspace, not in your source files, so they never end up in your code.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which key starts a debug session (or continues a paused one)?

    F5 starts debugging, and while paused it continues to the next breakpoint.
  2. 2. The paused line calls a function. Which command runs the whole call and stops on the next line?

    Step Over executes the call without entering it.
  3. 3. How do you set a breakpoint?

    A click in the gutter or F9 toggles a breakpoint on the current line.
  4. 4. What does the Call Stack show?

    The Call Stack lists active function calls from the current one down to the entry point.

Summary

  • A debugger pauses the program at breakpoints so you can inspect and control it.
  • Ctrl+Shift+D opens Run and Debug; F5 starts; F9 toggles a breakpoint.
  • F10 steps over, F11 steps into, Shift+F11 steps out, Shift+F5 stops.
  • Variables, Call Stack, Watch and the Debug Console show what is happening.
  • Place a breakpoint before the bug, then step forward to find the first wrong value.

Next: Breakpoint Types, Watch and the Debug Console