Breakpoints & Stepping — Debugging

A breakpoint is a mark on one line that makes the code stop (pause) before that line runs. Click a line number in the Sources panel to set one (or write…

A VAR referee (the video referee who checks the match recording again) doesn't judge a goal from one photo. The video is paused just before the moment, and they look at where every player stands. Then the video moves forward one frame at a time, until they find the foul.

A breakpoint works like the pause button in the VAR video, but for code. A breakpoint is a mark on one line that makes the browser stop just before that line runs. While it is stopped (paused), you can read the value of every variable at that moment, like the referee looking at where every player stands. Then you can move forward line by line (stepping), like the video moving forward one frame at a time. console.log gives you only one photo of one moment. A breakpoint gives you a pause button and a step button.

The program we debug

This cart should total 105,000: a Book at 50,000 with 10% off, times 2 (90,000), plus a Pencil at 5,000 times 3 (15,000). But the console prints NaN (Not a Number), and there is no red error at all.

function applyDiscount(price, percent) {
  const amountOff = price * percent / 100;
  return price - amountOff;
}

function calculateTotal(cart) {
  let total = 0;
  for (const item of cart) {
    const price = applyDiscount(item.price, item.discount);
    total += price * item.qty;
  }
  return total;
}

const cart = [
  { name: "Book", price: 50000, qty: 2, discount: 10 },
  { name: "Pencil", price: 5000, qty: 3 },
];
console.log(calculateTotal(cart)); // NaN

Line numbers in this lesson follow the code above: line 2 is const amountOff, line 9 is const price = applyDiscount(...), line 10 is total += ....

Set a breakpoint and pause

  1. Open DevTools (F12, or Ctrl+Shift+I; on a Mac Cmd+Option+I), then pick the Sources panel, the panel that shows the page's code files.
  2. In the file list on the left, open app.js (the file that holds the code above).
  3. Click line number 9. A blue marker appears: that's the breakpoint.
  4. Run the code again (reload the page, or click the button that triggers it).

The page stops, line 9 is highlighted, and the right sidebar says Paused on breakpoint. Remember: the highlighted line has not run yet. price only gets its value once you step forward.

The second way: write debugger; in the code. The browser stops on that line when DevTools is open, and ignores it when DevTools is closed.

Reading Scope and Call Stack

While paused, the right sidebar shows the two sections you'll use most: Scope (the variables you can use at that point, with their values) and Call Stack. Here is what they hold when the program stops inside applyDiscount for the Pencil:

Scope
  Local
    price: 5000
    percent: undefined
    amountOff: <value unavailable>
    this: Window
  Script
    cart: (2) [{…}, {…}]
  Global          Window

Call Stack
  applyDiscount   app.js:2
  calculateTotal  app.js:9
  (anonymous)     app.js:19

Want to watch one expression all the time? Add that expression under Watch, for example item.discount. Its value updates every time the code pauses.

Stepping: step over, step into, step out

Button Windows/Linux Mac What happens
Resume F8 Cmd+\ continue until the next breakpoint, or until the end
Step over F10 Cmd+' run this line to the end, including any function it calls, then stop on the next line
Step into F11 Cmd+; if this line calls a function, go in and stop on that function's first line
Step out Shift+F11 Cmd+Shift+; run the rest of this function, then stop again in the function that called it

This is the real trace when you start from a breakpoint on line 9:

Step Button Stops on What matters in Scope
1 reload the page line 9, the Book item total: 0
2 Step into line 2, in applyDiscount price: 50000, percent: 10
3 Step out line 10, back in calculateTotal price: 45000
4 Step over line 8, fetching the next item total: 90000
5 Step over line 9, the Pencil item item: {name: 'Pencil', price: 5000, qty: 3}
6 Step into line 2, in applyDiscount price: 5000, percent: undefined

From line 9, Step over runs applyDiscount to the end without going into it, then stops on line 10. Step into from the same line stops on line 2. One detail: when you step onto a return line, DevTools stops on it twice, before the value is calculated and then after it. At the second stop, Return value (the value the function gives back) appears in Local.

Conditional breakpoints and logpoints

A plain breakpoint on line 10 stops on every pass of the loop. With only two items that's fine. With 1,000 items, you press Resume 1,000 times.

price 45000 total 0
price NaN total 90000
NaN

The first two lines come from the logpoint, and the third from the console.log on line 19. The logpoint records the values before line 10 runs, so total still holds the old value. Breakpoints and logpoints are stored by DevTools, not in your files. They stay after a reload, and they never go into a commit (a saved set of changes in Git). That's why a logpoint fits code you must not or cannot edit, such as a library or a production build (the version of the code that visitors use).

Exercise: a mini conditional breakpoint

In the exercise, you build a small runtime (a mini program that runs steps) that copies how DevTools decides to stop. Breakpoints are stored as pairs of a line and a condition. The condition is a function that takes state and answers true or false. Each step(line, state) is recorded as a hit only when there is a breakpoint on that line and its condition is true.

The code below keeps those pairs in a Map, a list of key and value pairs: the key is the line number, the value is the condition.

function makeBreakpointRunner() {
  const breakpoints = new Map(); // line -> condition
  const hits = [];

  return {
    addBreakpoint(line, condition) {
      breakpoints.set(line, condition);
    },
    step(line, state) {
      const condition = breakpoints.get(line);
      if (condition && condition(state)) hits.push({ line, state });
    },
    getHits() {
      return hits;
    },
  };
}

In the exercise, traceStepThrough (already in the starter code) sets two breakpoints: on line 10 with the condition s.count > 5, and on line 20 with the condition !!s.error (s is the state). Then it runs six steps. !! turns any value into true or false.

Step Breakpoint on that line Condition Result
step(10, { count: 3 }) yes 3 > 5 false passes
step(10, { count: 7 }) yes 7 > 5 true hit 1
step(15, { count: 7 }) no passes
step(20, { error: null }) yes !!null false passes
step(20, { error: 'oom' }) yes !!'oom' true hit 2
step(10, { count: 9 }) yes 9 > 5 true hit 3

So the console prints 3 (the number of hits), 10 and 7 (the line and count of the first hit), 20 and oom (the line and error of the second hit), then 9 (the count of the third hit).

🎯 Other kinds of breakpoint

⚠️ Misunderstandings about pausing and stepping

TL;DR: A breakpoint is a mark on one line that makes the code stop (pause) before that line runs. Click a line number in the Sources panel to set one (or write debugger;). While paused, the highlighted line hasn't run yet. Scope shows the variables' values (Local, Block, Script, Global), and the Call Stack shows which function called which, read from the top. Resume (F8) continues to the next breakpoint. Step over (F10) runs one line, including any function it calls, without going into that function. Step into (F11) goes into the function the line calls, and Step out (Shift+F11) runs the rest of the function and goes back to its caller. A conditional breakpoint only stops when its condition is true, a logpoint prints to the Console without stopping, and neither changes your files. In the example program (a cart with a Book and a Pencil), the NaN appears because the Pencil has no discount: a breakpoint inside applyDiscount shows percent: undefined for the Pencil.

⚡ Set a breakpoint, the program stops

Write:

for (const item of cart) {
  const price = applyDiscount(item.price, item.discount);
  total += price * item.qty;
}
Guess first: a breakpoint (a stop mark) is set on the const price line, and the program stops there. Does price have a value yet?

Not yet. The line where the program stopped (the highlighted line) hasn't run. price only gets its value once you move forward one line.

Meaning: A breakpoint makes the program stop just before one line runs. Click a line number in the Sources panel (the code files panel in DevTools), then run the code again. The program stops on that line. Writing debugger; in the code does the same.

⚡ Scope and Call Stack

Meaning: While the program is stopped, Scope shows every variable's value at that moment. In the cart example, the total prints as NaN (not a number). applyDiscount(price, percent) calculates the price after the discount, and the Pencil item has no discount. So when the program stops inside applyDiscount for the Pencil, the Local section of Scope shows percent: undefined. A price times undefined gives NaN. The Call Stack, read from the top, shows which function called which.

⚡ Step over or step into

Guess first: while the program is stopped, the Step over button (F10) moves forward one line. You press it on the line that calls applyDiscount. Is that function skipped without running?

No. The function still runs to the end, you just don't go into it. Then you stop on the next line. Step into (F11) goes in and stops on the function's first line.

Meaning: Step out (Shift+F11) runs the rest of the function, then stops again in the function that called it. Resume (F8) continues to the next breakpoint.

⚡ Only stop when it matters

Write:

Number.isNaN(price)

Meaning: Type this condition into a conditional breakpoint (right-click the line number, then Add conditional breakpoint). DevTools only stops when the result is true, that is, when price holds NaN. So a long loop doesn't make you press Resume again and again. A logpoint prints values to the Console without stopping. Both are stored by DevTools, not in your files.

What you will learn