While paused in Chrome DevTools, how do you test a theory about a wrong value without reloading the page and reproducing the bug again?
answer
- the program is frozen, not finished
- double-click a value in Scope
- Watch re-evaluates as you step
- Console runs against the selected frame
- Restart frame moves only the pointer
basics
~20 sEdit the value in the Scope pane, track derived expressions in Watch, run code in the Console against the selected frame, then use Restart frame to run the function again. Restarting does not undo what the function already changed.
solid answer
~40 sOnce paused, I treat the stopped program as something I can question. The **Scope** pane lists Local, Closure and Global values, and double-clicking one changes it, so I can set the wrong total to the right value and resume to see whether the symptom goes away. **Watch** holds expressions I care about, such as `cart.items.length`, and re-evaluates them as I step and when I select another frame in the **Call Stack** pane. The **Console** also runs against the selected frame, so I can call a helper with different arguments. To replay, I right-click a frame and choose **Restart frame**, which moves execution back to the start of that function. It isn't time travel: anything the function already changed, including an argument it reassigned, stays changed, and async, generator and WebAssembly frames can't be restarted.
go deeper
Recall the panes: Scope shows the variables at the pause, Watch keeps expressions you add, and the Console can read local variables while paused. Know that a value can be edited by double-clicking it.
Explain that frame selection retargets Scope, Watch and the Console without moving execution, and that Restart frame moves only the pointer. Use the persisting argument or mutated object to show you understand what restarting does not undo.
Show how you use one reproduction to test several theories: edit a value to bracket the bug, replay with awareness of what was mutated, and treat side effects from the Console as real. Say when you must start clean instead.
This rarely needs a lead's decision. The angle worth making is cost: if reproducing a bug is slow enough that engineers depend on these tricks, investing in faster state setup may repay more than any debugger technique.
## Why this matters Reproducing a bug can be the expensive part of debugging: logging in, filling a cart, reaching the fifth item, and hoping the intermittent thing happens. Once you have finally landed on a paused line, the page's whole state is available. Chrome DevTools lets you question and change that state in place, so one successful reproduction can answer several questions. A **frame** here means one function's entry in the call stack, holding its own variables and current position. ## The Scope pane: read and change values The **Scope** pane in the **Sources** panel lists what the selected frame can see, grouped by where it comes from: - **Local** holds the current function's variables. DevTools can also add synthetic entries here, namely `Return value` when the pause sits on a function's return, and `Exception` when paused on one. - **Closure** holds variables captured from enclosing functions, labelled with the function's name where DevTools has one. - **Global** holds the page's global object. Double-click a value to edit it. This is the fastest test of a theory: if you think `total` should be `40`, set it to `40`, press `F8` to resume, and watch whether the screen comes out right. If it does, the bug is upstream of that line. If it does not, your theory was wrong and you spent ten seconds finding out. ## The Watch pane and frame selection **Watch** holds JavaScript expressions rather than single variables. Use **Add watch expression**, type something such as `cart.items.filter(i => !i.price).length`, and it stays there. Its value refreshes as you step and when you select a different frame. An expression that throws in the current frame shows `<not available>`, which is normal when a variable it names is not in scope there. Clicking a running frame in the **Call Stack** pane does not move execution. It changes **which frame you are looking at**, and Scope, Watch and the Console all follow. So you can inspect what the caller passed without stepping out. The **Console**, opened while paused, evaluates against that selected frame, so typed code can read its local variables and call functions in its scope. Anything you run there really executes, and it can change state. ## Restart frame: replay a function 1. Pause inside the function. 2. In the **Call Stack** pane, right-click the frame you want to replay. 3. Choose **Restart frame**. DevTools moves execution to the start of that function and pauses there. The important thing to understand is what it does **not** do. It does not restore state. It only moves the execution pointer back: ```js function applyDiscount(cart, rate) { rate = rate + 0.05; // grows again on every restart cart.total = cart.total * (1 - rate); // cart stays changed too debugger; } ``` Restarting `applyDiscount` keeps the increased `rate` and the already-reduced `cart.total`, so a second run applies a bigger discount to a smaller total. Restarting the **caller's** frame instead re-evaluates the call, so the caller supplies a fresh `rate` argument, but `cart` is an object and its change persists. Network requests already sent and DOM changes already made also stay done. The item is unavailable for async, generator and WebAssembly frames, and a restart can still fail if code you ran while paused has disturbed the stack. ## Which tool answers which question | Tool | Question it answers | Does it change the program? | |---|---|---| | **Scope** (reading) | What is this value right now? | No | | **Scope** (double-click) | Would the right value fix the symptom? | Yes | | **Watch** | How does this expression change as I step? | Only if the expression has side effects | | **Console** | What happens if I call this with other arguments? | It can, since the code really runs | | **Restart frame** | What happens if this function runs again? | It moves the position; earlier changes stay | Live editing goes one step further: while paused in the top-most function, you can edit its source in the editor and save, and DevTools applies the change and restarts that function. The same frame limits apply, plus the function cannot have recursive calls further down the stack. ## Mistakes to avoid - **Treating a restart as a clean rerun.** Check for mutated objects and arguments before trusting the second result. - **Watching an expression with a side effect.** `queue.shift()` removes an item each time Watch evaluates it. - **Calling a state-changing helper from the Console to "just check" it.** It runs for real. - **Trusting an edited run as proof of a fix.** Editing a value shows where the bug is not. The real change still has to be made in the source and tested from a clean start.
- Why did a value keep growing after each Restart frame?Restart frame does not restore state; it only moves execution back to the start of the function. If the function already reassigned an argument or changed an object, those values persist, and the next pass builds on them. Restarting the caller's frame gives a fresh argument value, because the call is evaluated again, but mutated objects stay mutated.
- What changes when you click a running frame lower in the Call Stack pane?Only your point of view. Scope, Watch and the Console now read that frame's variables, and the editor shows the line it is waiting on. Execution stays exactly where it was, and pressing a step key still continues from the top of the stack. It is how you see what a caller passed without stepping out.
- How can you see what a function is about to return in Chrome DevTools?Pause on its return, for example by stepping to the end of the function. DevTools then adds a synthetic `Return value` entry to the **Local** scope, which saves you from assigning the result to a temporary variable. You can double-click it to change what the caller receives, which is a quick way to test a theory about a bad result.
- Restart frame rewinds the program to the state it had when the function was called.
- Values in the Scope pane are read-only, so testing a theory means editing source and reloading.
- The Console always evaluates in global scope, even while execution is paused.
- A Watch expression is evaluated once, when you add it.
- Clicking a lower frame in the Call Stack pane rewinds execution to that frame.