SKU // 113

JavaScript Debugging Breakpoints

Logging shows you what happened after the fact. A breakpoint pauses your code while it runs, on the exact line you choose, so you can look at every variable, then step forward one line at a time and watch values change. You do not guess values with breakpoints; you see them.

// Setting a Breakpoint

Breakpoints are set in the browser's developer tools, not in your code:

  1. Open the developer tools (F12) and go to the Sources tab
  2. Open your script file from the list on the left
  3. Click a line number. A blue marker shows the breakpoint
  4. Reload the page (or run the code again) so the line is reached
  5. The code pauses before that line runs, and the line is highlighted

W3Schools' practice code calls a function four times. Put a breakpoint on the let result line and watch a, b, and result on each call:

function add(a, b) {
  let result = a + b;
  return result;
}

console.log(add(10, 5));
console.log(add(10, 50));
console.log(add(10, 500));
console.log(add(10, 5000));
15 60 510 5010

(W3Schools shows each result on the page with document.getElementById("demo").innerHTML = ..., which finds the element with id "demo" and replaces its content. console.log is simpler for practice.)

No HTML Page Needed
In Chrome, Sources, Snippets, + New snippet gives you a place to write code, set breakpoints, and run it with Ctrl+Enter (Cmd+Enter on a Mac). It is a great way to practice with the examples in these lessons.

// The debugger Keyword

Writing debugger; in your code works like a breakpoint on that line, but only while the developer tools are open. When they are closed, it does nothing:

let x = 15 * 5;
debugger;             // pauses here if the developer tools are open
console.log(x);
75
Watch out
Remove every debugger statement when you are done. Left in, it will pause the page for anyone who happens to have their developer tools open.

// Stepping Through Code

While paused, a row of buttons (and keyboard shortcuts) controls what runs next:

Button Chrome key What it does
ResumeF8Run normally until the next breakpoint
Step overF10Run the current line, including any function it calls, and stop on the next line
Step intoF11If the line calls a function, go inside it and stop on its first line
Step outShift+F11Finish the current function and stop where it was called

Step slowly and watch how the values change. Hovering over a variable in the code also shows its current value.

W3Schools error
It says to "use the play button to run your code line by line." The play button (Resume) runs the code until the next breakpoint. The Step buttons are the ones that go line by line.

// The Scope Panel

While paused, the Scope panel lists every variable available at the current line, grouped the way JS Scope describes them: Local (inside the current function), Block (inside the current { }), Closure (from an outer function), and Global.

let x = 10;
function test() {
  let y = 5;
  console.log(x + y);    // pause here: y is Local, x is under Script/Global
}
test();
15

// The Watch Panel

Add a variable name or any expression (like total / count or items.length) to the Watch panel, and its value updates every time you step. It is a cleaner alternative to adding many console.log calls.

// The Call Stack Panel

The Call Stack shows how you got to the current line: which function called which. Click any entry to jump to that spot and see the variables there. It is the same chain the stack property of an error shows (JS Error Object).

// Conditional Breakpoints and Logpoints

A breakpoint inside a loop stops on every repetition. Right-click the line number instead:

let total = 0;
for (let i = 1; i <= 10; i++) {
  total += i * i;       // try a conditional breakpoint: i === 7
}
console.log(total);
385

// Common Mistakes

// When to Use Breakpoints

Tool Best for
console.logA quick look at one or two values
BreakpointValues change unexpectedly, or you need to see many variables at once
Conditional breakpointA bug that only happens on one repetition of a loop
LogpointLogging without editing (or later cleaning up) your code

// Quick Check

The code is paused on a line that calls calculate(). Which button takes you inside calculate?

  1. Resume
  2. Step over
  3. Step into
  4. Step out
Show answer
C. Step over would run the whole function and stop on the next line; step out leaves the current function.

What does this log when the developer tools are closed?

let n = 2;
debugger;
n = n * 21;
console.log(n);
Show answer
42. With the tools closed, debugger does nothing, and the code runs straight through.

A bug only happens when i is 500 in a loop that runs 1000 times. How do you pause at exactly that point?

Show answer
Right-click the line number and add a conditional breakpoint with the condition i === 500. It will only pause when that is true.

// Try It

  1. Click Open Playground. Open your browser's developer tools (F12), then click Run: the code pauses on the debugger line
  2. Step through both calls, watching a, b, and result in the Scope panel
  3. Open the loop example with its Try it Yourself button, add debugger; inside the loop, and add total to the Watch panel
  4. Set a conditional breakpoint that pauses only when i === 7
// Video Walkthroughs
Last updated: Oct 10, 2026, 1:50 AM ET