skip to content

In Chrome DevTools, a bug hits only the fifth item of a list: would you use a conditional breakpoint or a logpoint, and why?

level: middleimportance: should knowfreq 52%

answer

  1. a plain breakpoint stops on every item
  2. expression evaluated on each hit
  3. pause versus print and carry on
  4. pausing can hide a timing bug
  5. the expression can change state

basics

~20 s

Use a conditional breakpoint to pause only when an expression such as index === 4 is true. Use a logpoint to print values on every hit without pausing, when you need the sequence or a pause would hide the bug.

solid answer

~50 s

A plain breakpoint in a loop stops on every item, so I'd right-click the line number, choose **Add conditional breakpoint…** and enter something like `index === 4`. The expression is evaluated in that line's scope each time execution reaches it, and DevTools pauses only when it is truthy, so I land on the fifth item with **Scope** and the **Call Stack** ready. I'd pick **Add logpoint…** when I don't yet know which item is wrong, or when pausing would change the bug, as with a timing race or a focus state. A logpoint takes `console.log`-style arguments and prints to the **Console** on each hit without stopping, and neither kind needs a rebuild. They aren't free, though: the expression runs on every hit, so a heavy one slows a hot loop, and one with side effects changes the program you are debugging.

code

javascript · 5 lines
javascript
items.forEach((item, index) => {
  // Conditional breakpoint on the next line: index === 4
  // Logpoint on the next line: 'item', index, item.price
  addToTotal(item.price);
});

go deeper

for a junior

Recall that both are added by right-clicking a line number, that one pauses only when an expression is true, and that the other prints to the Console without pausing at all.

for a middle

Explain the mechanism: the expression runs in the line's scope on every hit, and truthiness decides the pause. Then give a reason to prefer each kind, including that a pause can change the behaviour of a timing bug.

for a senior

Show judgement about observation effects: what a pause disturbs, what a hot-path condition costs, and how a condition on the data beats one on the index. Mention using these on a deployed build where a rebuild is not an option.

for a principal

There is little to decide here beyond team habit. The useful angle is coaching: engineers who reach for logpoints stop committing debug output, and the time saved on reproducing a list-position bug is easy to demonstrate.

## Why a plain breakpoint is the wrong tool here A **line-of-code breakpoint** stops every time execution reaches its line. Inside a loop over a long list, that means pressing resume four times to reach the fifth item, and doing it again after every reload. Chrome DevTools offers two variants that attach an expression to the same line, and the choice between them comes down to one question: do you need to stop, or do you only need to see? Both are real breakpoints from the debugger's point of view, so neither touches the source, neither needs a rebuild, and both work on code that is already deployed. ## Conditional breakpoints 1. Right-click the line number in the **Sources** panel. 2. Choose **Add conditional breakpoint…**. The dialog's placeholder reads `Expression to check before pausing, e.g. x > 5`. 3. Type the expression and press `Enter`. The mechanism is worth stating exactly: - The expression is evaluated **in the scope of that line**, so it can read the local variables there. - It is evaluated **every time** execution reaches the line, not once when you set it. - DevTools pauses only when the result is **truthy**. Otherwise execution carries on with no visible stop. For the fifth item of a zero-based `forEach`, the condition is `index === 4`. If position is not what matters, a condition on the data is often better, such as `item.price == null`. In a bundled file, read the available variable names off the **Scope** pane while paused at that line once, then write the condition with those. ## Logpoints Right-click the line number and choose **Add logpoint…**. The dialog's tooltip says it plainly: `Log a message to Console, don’t break`. You type what you would pass to `console.log`, comma-separated, such as `'item', index, item.price`. Each hit prints a line to the **Console** and execution continues. Use one when you want the *sequence* rather than a frozen moment, for example to spot which of forty items is the first to go wrong. Use one when a pause would disturb the bug. A pause freezes the page's JavaScript while the rest of the world moves on: responses arrive, and moving to DevTools can take focus from the page, so a focus or hover bug may vanish the moment you stop. ## Choosing between them | Question | Conditional breakpoint | Logpoint | |---|---|---| | Does execution stop? | Yes, when the expression is truthy | No, never | | What you get | Live Scope, Call Stack, stepping | Values printed in the Console | | Best when | You know which iteration matters | You need the sequence, or a pause hides the bug | | Cost on each hit | The expression is evaluated | The arguments are evaluated and printed | | Can you edit state there? | Yes, while paused | Only through side effects in its arguments | You can switch a breakpoint's kind later without deleting it: **Edit breakpoint…** opens the dialog, whose drop-down offers **Breakpoint**, **Conditional breakpoint** and **Logpoint**. ## Tricks that follow from the mechanism The breakpoint dialog has one expression field and no hit-count setting, but an expression can do the counting. Two useful forms: ```js // Pause on the fifth time this line runs, when no index is in scope (window.__hits = (window.__hits || 0) + 1) === 5 // Log only the interesting item: console.log returns undefined, so this never pauses index === 4 && console.log(item) ``` The first deliberately writes a global, so reset it before you reproduce again. **Never pause here** sits in the same right-click menu. It adds a breakpoint whose condition never lets it pause, which is how you quiet a line that keeps stopping you, such as a stray `debugger;` statement in code you cannot rebuild right now. ## Mistakes to watch for - **An accidental assignment.** `index = 4` is truthy and also overwrites the loop variable. It pauses every time and corrupts the run you are studying. - **Expensive expressions on hot paths.** A condition on a line that runs thousands of times per frame adds real work and can change the timing you came to look at. - **Side effects in a logpoint.** Whatever you type is executed, so a call that mutates state changes the behaviour you are observing. - **Logging objects.** The Console holds a live reference, so an object you expand later can show its current state rather than its state at that hit. Log the specific fields you care about.

  • How do you stop on the fifth call when no loop index is in scope?
    Make the condition count. `(window.__hits = (window.__hits || 0) + 1) === 5` increments a global on each hit and is truthy only on the fifth. The breakpoint dialog has one expression field and no hit-count setting, so this is the usual substitute. It is a deliberate side effect, so reset the counter before reproducing again.
  • Why can pausing make a bug disappear, and how does a logpoint help?
    A pause freezes the page's JavaScript while the outside world keeps going: responses arrive, and moving to DevTools can take focus away from the page. A race or a focus-dependent bug then behaves differently under observation. A logpoint never stops execution, so the order of events stays much closer to the real run, though each hit still costs a little time.
  • What does Never pause here do in Chrome DevTools?
    It is an entry in the line number's right-click menu that adds a breakpoint with a condition that never allows a pause. It is useful for a line that keeps stopping you and that you cannot change right now, such as a stray `debugger;` statement in a deployed bundle. Removing that breakpoint restores the old behaviour.

  • A logpoint pauses for a moment and then continues automatically.
  • A conditional breakpoint's expression is evaluated once, at the moment you set it.
  • A breakpoint condition runs in global scope, so it cannot read local variables.
  • Breakpoint expressions are read-only, so they cannot change the program's state.
  • The breakpoint dialog has a hit-count field, so you just type 5 into it.
  • A logpoint has to be added to the source and rebuilt, like console.log.