Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Pause Your Script in the Debugger

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 top and 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 as counts. Inside a function, Local holds its parameters. In progressLine(), that’s its own category, 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 showed Clothes: 0. One Step over later, it showed Clothes: 1, and the highlight was back on the for line, 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 the console.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:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Last time, you found a bug by adding log lines, reloading and reading the Console, again and again. In the full packing list, with several files, each guess costs another log line and another reload.

00:14 Now you’ll stop the script on a line and read every value at that moment. You’ll use the same practice folder, progress-bug, as last lesson left it, fixed.

00:24 One thing is new. A small function, progressLine, builds each output line, so there’s a function to step into. Copy the new file from below this video, so your line numbers match.

00:37 Open the page with Live Preview and open DevTools. Then select the Sources tab. On its left are the files the page loaded. This toy copy has three. Open progress.js in the editor.

00:51 Line 10 adds one to a category’s count. Click its line number. A blue marker appears. That’s a breakpoint, a line where the browser will pause your script.

01:01 The script already ran, so reload the page. This time, the page stops loading. It shows Paused in debugger, and line 10 is highlighted.

01:13 On the right, the Scope pane lists what your code can see right now. Under Block, the loop’s item is the swimsuit. Under Module, the file’s own names include counts, with a zero for each category.

01:26 Hovering over counts in the code shows the same value.

01:31 Look at Clothes. It’s zero, right beside a line that adds one to it. It’s tempting to read that zero as the line’s result, and decide the line is broken. Has it run yet?

01:43 Your turn: pause the video and predict what Clothes will be after one Step over.

01:52 It’ll be one. Click Step over, the curved arrow. The highlight jumps back up to the for line, ready for the next item, and Clothes is now one. The line wasn’t broken.

02:05 It hadn’t run yet. The highlight marks what runs next, so the debugger is always paused before it.

02:12 Resume runs your script until it reaches a breakpoint again. The next item is the sun hat. Your turn: pause the video and predict which item the script will pause on next.

02:28 It pauses on the passport.

02:31 The sun hat, the sunscreen and the toothbrush aren’t packed, so line 10 never ran for them. A breakpoint only pauses a line that runs. Clothes is still one, and Documents is still zero, because line 10 hasn’t run for the passport yet.

02:48 Click line 10’s marker to remove it, and click line 22 to set a new one. Then click Resume. The script pauses on line 22, where category is Clothes.

03:00 Line 22 calls the progressLine() function. Step over would run all of it at once. Step into goes inside it instead. Click it, and the highlight moves to the function’s first line, line 15.

03:13 The Scope pane now has a Local section, where the function’s own category, its parameter, is Clothes too.

03:22 Step out runs the rest of the function and returns to the line that called it. Click it, and you’re back on line 22, with the function returned and only the log left.

03:32 Press Escape to open the Console below. One more Step over runs the log, and the Console shows Clothes, 1 of 2 packed.

03:42 A debugger line in your code pauses your script too, but only while DevTools is open. With DevTools closed, the page runs straight through. As a rule, the highlight marks what runs next, and it hasn’t run yet.

03:56 Step over runs what’s highlighted, calls and all. Step into goes inside a call, and Step out finishes it. Resume runs to the next breakpoint. Before each click, say what you expect, then check the Scope pane.

04:11 To finish, remove the marker and click Resume. In Find a Bug With Breakpoints, you’ll use these buttons to hunt down a wrong value in Alex’s packing list.

Become a Member to join the conversation.