In this video, you paused the practice copy of the packing list’s progress count in Chrome’s debugger and read its values without adding a single log line.
Key Points
- Open the debugger: open DevTools and select the Sources tab. Its left side lists the files the page loaded, under
topand the server’s address. Open the one you want in the editor. - Set a breakpoint: click a line number. A blue marker appears. Reload the page, and the script pauses when it reaches that line. The page shows Paused in debugger, and the line is highlighted.
- Read the values: the Scope pane lists what your code can see at that moment. Block holds a loop’s own variables, such as
item. Module holds the file’s own names, such ascounts. Inside a function, Local holds its parameters. InprogressLine(), that’s its owncategory, which got the loop’s value but is a separate variable. Hovering over a name in the code shows the same value. - The highlighted code hasn’t run yet. Paused on
counts[item.category] += 1;, the Scope pane showedClothes: 0. One Step over later, it showedClothes: 1, and the highlight was back on theforline, ready for the next item. The highlight marks what runs next, so the debugger is always paused before it. Usually that’s the start of a line, but it can be partway through one. After Step out,progressLine(category)on line 22 had already returned, and only theconsole.log()was left to run. - The four buttons:
- Step over runs what’s highlighted, calls and all, without going inside them.
- Step into goes inside the function the line calls.
- Step out runs the rest of the function and goes back to the line that called it.
- Resume runs until the script reaches a breakpoint again.
- A breakpoint only pauses a line that runs. With the breakpoint on line 10, Resume skipped the sun hat, the sunscreen and the toothbrush, because they aren’t packed, and paused on the passport.
- Predict, then click, then check: before each click, say what you expect, then check the Scope pane.
debugger;in your code pauses the script there too, but only while DevTools is open. With DevTools closed, the page runs straight through.- The Console drawer: press Esc in DevTools to open the Console under the editor, so you can watch the output without leaving the Sources tab.
- To finish, click the marker to remove it and click Resume.
The Code
progress-bug/progress.js is last lesson’s fixed version, with the progress line now built by a small function, progressLine(), so there’s a function to step into. The breakpoints went on line 10 and line 22, and Step into landed on line 15. Replace your progress-bug/progress.js with this file, blank lines included, so your line numbers match the video’s: