T3 Browser DevTools · Lesson 4
The Sources panel and breakpoints
Going inside the running code: pausing with breakpoints and the debugger statement, reading every value in the Scope pane, stepping over, into and out, choosing the right kind of breakpoint, and finding the bugs that logs would hide.
- find your way round the Sources panel, and open any file a page loaded
- pause a script, and read every value at that moment
- step through code, into functions and out of them
- choose the right kind of breakpoint for what you know
- decide when to log and when to pause, and find a silent bug
The back cover
Logs tell you what you thought to ask. Some bugs hide exactly where you did not think to look: a number that is text, a function called twice, a value that changes behind your back. To find those, you need to stop the code and look at everything at once.
These pages show the Sources panel and its breakpoints. They show how to pause with a breakpoint or the debugger keyword, and how to read every value in the Scope pane. They show how to step over, into and out, and which kind of breakpoint suits which question. They end with a method for the silent bugs that logs would hide.
Before you start
The story: Notes on every line
The tram office's fare panel charges small children the adult fare, though not all of them. A boy of seven paid full price this morning, and a girl of eleven paid half. Corin has been at the script since breakfast. He has added a note to every line, and the diary now scrolls past too fast to read.
Ottilie Sen watches him for a while. Then she sets a fourth lens on the bench, with a small brass pin through its rim.
"You are listening at the door," she says. "Go in. Stop the machine, and stand beside it while it works."
The Console tells you what a script said. It cannot show you what the script was thinking. When a script runs to the end with a wrong answer and no error, you can scatter logs and guess, or you can stop the script in the middle and look at everything it holds at that moment. This panel lets you do that. It builds on these ideas; each has a tiny example.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| A script runs one line at a time. | Line 1, then line 2. | You can stop it between two lines. |
| A function runs when called. | fare(15, 4) | You can step into it, or over it. |
| A variable holds a value. | const sum = 60; | The panel shows each variable's value as you go. |
| Text and numbers differ. | "7" is text, and 7 is a number. | The panel shows text in quotation marks. |
| A stack trace shows who called what. | fare, called from the top level. | The Call Stack pane shows it live. |
| console.log prints a value. | console.log("sum", sum) | A logpoint does this with no change to your file. |
| Events run code later. | A click runs its handler. | You can pause on the event itself. |
| A selector finds an element. | document.querySelector(".out") | You can pause when an element changes. |
By the end of this lesson you will be able to:
- find your way round the Sources panel, and open any file a page loaded;
- pause a script with a breakpoint or a debugger statement, and read every value at that moment;
- step through code line by line, into functions and out of them;
- choose the right kind of breakpoint: conditional, logpoint, exception, DOM change, event and fetch;
- decide when to log and when to pause, and find a bug that logs would hide.
How to follow this lesson
Open DevTools before you press Run on a block below. Each block with the word debugger in it will stop by itself, and the Sources panel will come to the front. Press F8 to let it carry on. If DevTools is closed, the word debugger does nothing. Under each block is what the kit saw when it attached Chrome's debugger, in the same way as the panel does.
In one line
The Sources panel stops a script while it runs, so that you can read everything it holds; you need the idea of a function, a variable and a call.
The room with the machine in it
At a glance
So far you have stood outside the machine: Elements shows what it built, the Console what it said, and Network what it was sent. The Sources panel takes you inside, to the code itself, while it runs.
It has three parts: the file tree, the Code Editor and the Debugger, and Ctrl + P opens any file the page loaded. Its great gift is the breakpoint, which stops the script at a line. A log shows only what you thought to ask for; a pause shows everything.
Take a guess: What can a breakpoint show you that console.log cannot?
So far you have stood outside the machine. The Elements panel shows what it built, the Console shows what it said, and the Network panel shows what it was sent. The Sources panel takes you inside, to the code itself, while it runs.
A DevTools panel that shows every file the page loaded, and lets you stop the page's JavaScript at any line, look at its values, and move forward a line at a time.
| Part | Where | What it does |
|---|---|---|
| the file tree | left, on the Page tab | lists every file the page loaded, by address |
| the Code Editor | middle | shows the chosen file, with line numbers |
| the Debugger | right, or below on a narrow window | Breakpoints, Scope, Watch and the Call Stack, with the buttons to step |
| To do this | Windows or Linux | Mac |
|---|---|---|
| open a file by name | Ctrl + P or Ctrl + O | Cmd + P or Cmd + O |
| go to a line | Ctrl + G | Ctrl + G |
| pause or resume | F8 | F8 |
| step over | F10 | F10 |
| step into | F11 | F11 |
| step out | Shift + F11 | Shift + F11 |
You will not always know which file to open. Ctrl + P finds a file by a few letters of its name, which is quicker than the tree. The click on the link beside any Console message, or any rule in the Styles tab, opens the same place.
Why not only console.log?
| console.log | A breakpoint | |
|---|---|---|
| you must change your file | yes, and later change it back | no |
| you must choose what to look at first | yes: only what you logged | no: everything is there |
| you see values | as they were when logged | live, and you can change them |
| you can follow the path the code takes | only by adding more logs | yes, a line at a time |
| needs a reload or a re-run | each time you add a log | no, for code that runs on a click |
| good for | a quick look, or code that runs many times | not knowing where to look |
Neither replaces the other. A log is a question you already know how to ask. A breakpoint is for when you do not yet know the question.
A memory aid
Log what you know to ask. Pause when you do not.
Blocks in these lessons
A block's page runs inside a frame, so in the file tree it appears as about:srcdoc, under a frame's name. The runner's own short script comes first in it, and yours follows. So the line numbers you see there differ from the block's. That is why this lesson names lines by what they say.
When you are stuck: the Sources panel
You cannot find a file: press Ctrl + P and type part of its name.
The right-hand part is missing: press Ctrl + Shift + H, or widen DevTools.
A file looks like one enormous line: press the {} button at the foot of the editor to format it.
Your turn
Which key resumes a paused script?Tap to reveal
- Eight.
F8.
Which is better when you do not yet know where the bug is: a log or a breakpoint?Tap to reveal
- A log is a question you know how to ask.
A breakpoint.
Why does a block's line number in Sources differ from the block as shown?Tap to reveal
- Something comes first.
The runner's own script comes before it.
Did you know?
Source maps, a standard since 2024
The code a browser runs is often not the code that was written, because tools compile it first. A source map links the two, so that developer tools can show the original. Its format became an Ecma standard, ECMA-426, in a first edition of December 2024. It aims at source-level debugging with mapping in both directions, for code compiled to JavaScript, WebAssembly and CSS.
Why it fits here: If the Sources panel shows your original files on a built site, a source map is doing that work.
Source: Ecma International: ECMA-426
Changes that survive a reload
Edits made in DevTools vanish when you reload, unless you ask DevTools to keep them. With local overrides, DevTools saves a copy of the modified file to a folder you choose. When you reload the page, DevTools serves that local file instead of the one from the network. Chrome's documentation suggests it for testing fixes before they go to production, without waiting for the backend.
Why it fits here: It answers a common wish: to try a change to a page's file and keep it across reloads, with no server of your own.
Source: Chrome for Developers: Override web content and HTTP response headers locally
In one line
The Sources panel has the file tree, the Code Editor and the Debugger; Ctrl + P opens any file, and a breakpoint shows you everything, where a log shows only what you thought to ask for.
Stopping the machine
At a glance
A cricket umpire can stop the game, and for that moment nothing moves: the players stay where they were, and the score is what it is. A script stopped at a breakpoint is like that. You can walk round and look.
You set a breakpoint by writing debugger in the code, or by clicking a line number. The line has not run yet when the script pauses. The Scope pane then shows every value, with text in quotation marks, and the Call Stack shows how the code got there.
Take a guess: When a script pauses on a line, has that line run yet?
A cricket umpire can stop the game, and for that moment nothing moves: the players are where they were, and the score is what it is. You can walk round and look. A script stopped at a breakpoint is like that.
A mark on a line of code that makes the browser pause just before that line runs. While paused, nothing on the page moves, and every value the script holds can be read.
The word debugger
The plainest way to pause is to write the word debugger in your code. With DevTools open, the script stops there. With DevTools closed, the word does nothing.
<!DOCTYPE html>
<p class="out"></p>
<script>
function fare(price, people) {
const sum = price * people;
debugger;
return sum;
}
document.querySelector(".out").textContent = fare(15, 4);
</script>
The debugger shows
Paused at: debugger;
call stack: fare, called from (top level)
price = 15
people = 4
sum = 60
Checked in Chrome
- with DevTools closed, the word does nothing, and the page shows 60
Open DevTools, press Run, and the panel comes to the front with the line highlighted. Read what you can see. In the Scope pane are price, people and sum with their values. In the Call Stack pane are fare, and below it the top level that called it. A banner on the page says Paused in debugger. Press F8 to go on.
Leaving debugger in your code
Any visitor who has DevTools open will have your page stop dead. Take the word out when you have finished, or better, use a breakpoint, which is never in your file.
Line-of-code breakpoints
A breakpoint needs no change to your file. Click a line's number in the Code Editor, and a blue marker appears. Click it again to remove it. The marker stays through reloads, and the Breakpoints pane lists them all.
A script that has already finished cannot be paused, so a line that runs at load needs a reload after you set the marker. Code that runs on a click needs no reload: set the marker, then click.
<!DOCTYPE html>
<button>Add a tea</button>
<p class="out">₹0</p>
<script>
let count = 0;
function add() {
count = count + 1;
document.querySelector(".out").textContent = "₹" + count * 15;
}
document.querySelector("button").addEventListener("click", add);
</script>
The debugger shows
Paused at: count = count + 1;
call stack: add
count = 0
Checked in Chrome
- one click gives ₹15
Run that block, and find its file in the tree. Click the line number beside count = count + 1;, then press the page's button. The script stops before that line runs, which is why count still reads 0. A breakpoint always pauses just before its line.
A memory aid
Paused on a line means that line has not run yet.
The Scope pane: everything it holds
The Scope pane lists the variables this code can reach, in groups.
| Group | What it holds |
|---|---|
| Local | the current function's own variables and arguments |
| Block | variables of the current loop or if, where there are any |
| Closure | variables of the functions outside this one |
| Script | top-level let and const of the page's scripts |
| Global | everything on window: hundreds of things, usually closed |
Text appears in quotation marks, and numbers do not, which matters more than it sounds. Hold the pointer over any variable in the Code Editor to see its value in place, and you need not look away from the code.
The Call Stack: how you got here
The Call Stack pane is the stack trace from the Console, live. The top line is where you are; each line below it called the one above. Click a line, and the editor and the Scope pane jump to that function, as it stood when it made the call, so you can see what it passed.
Worked example 1
A fare calculator shows ₹600 for 4 people at ₹15 each. Pause it just before it returns, and find out which value is wrong.
What we have: a wrong total, and a function that works it out
What we need: the values inside the function at that moment
The plan: put debugger before the return, and read the Scope pane
- Add
debugger;before the return, open DevTools, and run. - The Scope pane reads
price = 150,people = 4,sum = 600. The sum is right for those values, so the fault is in what came in. - Click the line below in the Call Stack. The caller passes 150. Someone typed an extra zero.
<!DOCTYPE html>
<p class="out"></p>
<script>
function fare(price, people) {
const sum = price * people;
debugger;
return sum;
}
document.querySelector(".out").textContent = fare(150, 4);
</script>
The debugger shows
Paused at: debugger;
call stack: fare, called from (top level)
price = 150
people = 4
sum = 600
Checked in Chrome
- the page shows the wrong 600
price was 150, not 15. The function was innocent; the caller passed the wrong number.
Check: the debugger read price as 150 inside fare, and the Call Stack names who passed it.
When you are stuck: pausing
It does not stop: is DevTools open? Does that line ever run? Are breakpoints deactivated in the Debugger's top row?
The value is undefined: you are paused before the line that sets it; step once.
A line runs at load, and your breakpoint came too late: reload.
Your turn
Paused on const x = 5;, what is x?Tap to reveal
- The line has not run yet.
Not set yet: the line has not run.
How do you remove a line-of-code breakpoint?Tap to reveal
- The same way you made it.
Click the line number again.
Which pane lists who called the current function?Tap to reveal
- A stack.
The Call Stack pane.
Did you know?
A keyword that may do nothing
The keyword debugger belongs to JavaScript's own standard, but the standard promises little. Evaluating it may allow an implementation to cause a breakpoint when run under a debugger. If a debugger is not present or active, the statement has no observable effect. So a debugger line forgotten in a file does nothing to visitors who never open DevTools, and pauses only for those who do.
Why it fits here: It is why debugger is safe to try in these blocks, and why it should still be taken out before a page goes live.
A breakpoint in one byte
Debuggers of native programs can set a breakpoint with a single byte. On x86 processors, the INT3 instruction is a one-byte instruction defined for use by debuggers: they put it in place of an instruction of the running program, for a while, to set a code breakpoint. Microsoft's documentation shows the same idea behind __debugbreak(), its portable way to cause a breakpoint, which on x86 is similar to int 3.
Why it fits here: It shows how old and plain the idea of this topic is: mark a place, and the machine stops there for you.
Sources: Wikipedia: INT (x86 instruction); Microsoft Learn: __debugbreak
In one line
A breakpoint pauses just before its line: write debugger, or click a line number. While paused, the Scope pane shows every value, with text in quotation marks, and the Call Stack shows how the code got here.
One tooth at a time
At a glance
A paused machine is useful, and a machine you can turn by hand is better. The buttons at the top of the Debugger move the script forward by exact amounts, and the Scope pane updates at each step.
Step over runs a line and stays here. Step into goes inside the function the line calls, and step out finishes that function. Resume runs on to the next breakpoint. While paused, the Console runs inside the paused code, so you can ask about any value it holds.
Take a guess: If the paused line calls a function you trust, which button skips over it?
A paused machine is useful. A machine you can turn by hand is better. The buttons at the top of the Debugger move the script forward by exact amounts, and the Scope pane updates at each step.
| Button | Key | What it does | Use it when |
|---|---|---|---|
| Resume | F8 | runs until the next breakpoint, or the end | you have seen enough here |
| Step over | F10 | runs this line, then stops at the next, without going inside any function it calls | the function on this line is not the problem |
| Step into | F11 | goes inside the function this line calls, and stops at its first line | the problem may be inside it |
| Step out | Shift + F11 | runs the rest of this function, and stops back in the function that called it | you have seen enough of this function |
A memory aid
Over: skip it. Into: go in. Out: get me out.
Walking through a call
Here is a bill with 5% tax. Pause at the line that calls bill, then step into it twice, to reach tax.
<!DOCTYPE html>
<p class="out"></p>
<script>
function tax(amount) {
return amount * 0.05;
}
function bill(amount) {
const extra = tax(amount);
const total = amount + extra;
return total;
}
const answer = bill(200);
document.querySelector(".out").textContent = answer;
</script>
The debugger shows
Paused at: const answer = bill(200);
call stack: (top level)
Paused at: const extra = tax(amount);
call stack: bill, called from (top level)
amount = 200
Paused at: return amount * 0.05;
call stack: tax, called from bill, called from (top level)
amount = 200
Paused at: const total = amount + extra;
call stack: bill, called from (top level)
extra = 10
Paused at: return total;
call stack: bill, called from (top level)
total = 210
Paused at: document.querySelector(".out").textContent = answer;
call stack: (top level)
answer = 210
Checked in Chrome
- the page shows 210: 200 plus 5%
Follow the stops. Each step into adds a line to the Call Stack, and step out takes one away. Step out ran the rest of tax and stored its answer, so Chrome stopped at the next line of bill, with extra reading 10. The last step over runs the return, and takes you back to the top level, with the answer in place.
Three ways to go further
| Tool | What it does |
|---|---|
| Continue to here | right-click a line while paused: runs up to it, with no breakpoint left behind |
| The Watch pane | add an expression such as total > 200: it is worked out afresh at every step |
| The Console | while paused, the Console runs in the paused code, so you can type any variable, or try a sum |
The Console while paused is one of the most useful things in all of DevTools. You are standing inside the function, with all its variables. Type amount * 0.18, and you see what a different rate would give. You can even change things: double-click a value in the Scope pane and type a new one, and the rest of the script carries on with it. It is a way to test a fix without touching a file.
Worked example 2
A bill for ₹200 with 5% tax comes to ₹300. Step through the code and find the line that goes wrong.
What we have: a bill that is ₹90 too large
What we need: the first value that differs from what you expect
The plan: pause at the call, step in, and say what you expect before each step
- Pause at
const answer = bill(200);. PressF11to go into bill: amount reads 200, as expected. - Press
F10to step over the tax line. You expect extra to be 10. It reads 100. - So the fault is in tax. Back up: reload, step into bill, then into tax, and read the line. It says 0.5, not 0.05.
<!DOCTYPE html>
<p class="out"></p>
<script>
function tax(amount) {
return amount * 0.5;
}
function bill(amount) {
const extra = tax(amount);
const total = amount + extra;
return total;
}
const answer = bill(200);
document.querySelector(".out").textContent = answer;
</script>
The debugger shows
Paused at: const answer = bill(200);
call stack: (top level)
Paused at: const extra = tax(amount);
amount = 200
Paused at: const total = amount + extra;
extra = 100
Paused at: return total;
total = 300
Checked in Chrome
- the page shows the wrong 300
0.5, not 0.05. The first surprise was extra = 100, and it pointed straight at tax.
Check: the debugger read extra as 100 where 10 was expected.
Worked example 3
A shop's notice says free delivery from ₹499. An order of exactly ₹499 is charged ₹40. Step through the rule, and see which line runs.
What we have: a rule that fails at exactly one value
What we need: the path the code takes for 499
The plan: pause at the call, step in, and step over the if: the highlighted line shows which way it went
- Pause at the call, and press
F11to go in. total reads 499. - Press
F10on the if. The highlight jumps toreturn 40;, so the condition was false. - Read the condition:
total > 499. Is 499 above 499? No. The notice says from ₹499, so the test must be>=.
<!DOCTYPE html>
<p class="out"></p>
<script>
function delivery(total) {
if (total > 499) {
return 0;
}
return 40;
}
const fee = delivery(499);
document.querySelector(".out").textContent = fee;
</script>
The debugger shows
Paused at: const fee = delivery(499);
call stack: (top level)
Paused at: if (total > 499) {
call stack: delivery, called from (top level)
total = 499
Paused at: return 40;
call stack: delivery, called from (top level)
Checked in Chrome
- the order of ₹499 is charged 40
<!DOCTYPE html>
<p class="out"></p>
<script>
function delivery(total) {
if (total >= 499) {
return 0;
}
return 40;
}
const fee = delivery(499);
document.querySelector(".out").textContent = fee;
</script>
Checked in Chrome
- with >=, the order of ₹499 goes free
> should be >=. Stepping showed the path: the if was false, and the next line run was return 40.
Check: the debugger stopped on return 40 after the if, and the fixed rule gives 0.
When you are stuck: stepping
You stepped into a library's code: press Shift + F11 to get out, or step over such lines.
You went past the line: there is no step back; reload, or right-click a function in the Call Stack and choose Restart frame.
F10 or F11 changes the screen's volume: on many laptops, hold Fn with it, or use the buttons.
Your turn
You are on a line that calls a function you trust. Which step do you use?Tap to reveal
- Skip it.
Step over, F10.
You are inside a function, and have seen enough. Which step?Tap to reveal
- Get me out.
Step out, Shift + F11.
While paused, where can you try a sum using the function's own variables?Tap to reveal
- It runs in the paused code.
In the Console.
Did you know?
The same keys elsewhere
The step keys are shared beyond Chrome. In Chrome DevTools, F10 steps over the next function call, F11 steps into it, and Shift + F11 steps out of the current function. Microsoft's Visual Studio uses the same three keys: F10 for Step Over, F11 for Step Into and Shift + F11 for Step Out. Learn them once, and they work in both.
Why it fits here: The buttons of this topic have keys, and the keys travel: the same moves work in other debuggers too.
Sources: Chrome for Developers: Keyboard shortcuts; Microsoft Learn: Navigate through code with the Visual Studio debugger
next, step and finish
Command-line debuggers have the same three moves under other names. In GDB, the GNU debugger, the command next goes on to the next source line, and function calls within that line run without stopping. The command step goes inside any function called within the line. The command finish runs until the current function returns, then prints the returned value. GDB's manual names Richard Stallman as its original author.
Why it fits here: Over, into and out are not Chrome's inventions: debuggers share them, so what this topic teaches works far beyond the browser.
Sources: GDB manual: Continuing and Stepping; GDB manual: Contributors to GDB
In one line
Step over runs a line and stays here, step into goes inside the function it calls, step out finishes the function, and resume runs on. While paused, the Console runs inside the paused code.
The right pin for the job
At a glance
A line-of-code breakpoint needs you to know the line, and often you do not. You know what happens, but not where. Each kind of breakpoint starts from something you do know.
A conditional breakpoint stops only in the one case that matters, and a logpoint prints a value without stopping or changing the file. Pause on exceptions stops at the line that throws. DOM change breakpoints catch whoever touches an element, event listener breakpoints catch what runs on a click, and fetch breakpoints catch whoever asked for an address.
Take a guess: Which breakpoint finds the line that changes an element, when you do not know where it is?
A line-of-code breakpoint needs you to know the line. Often you do not. You know what happens, and not where. Each kind of breakpoint below starts from something you do know.
| You know | Use | Set it in |
|---|---|---|
| the line | a line-of-code breakpoint | the line number |
| the line, and when it goes wrong | a conditional breakpoint | right-click the line number, or Ctrl + click it |
| the line, and only want to read a value | a logpoint | right-click the line number, or Ctrl + Shift + click it |
| that an error is thrown | pause on exceptions | the Breakpoints pane |
| which element changes | a DOM change breakpoint | the Elements panel: right-click the node, Break on |
| what the visitor did | an event listener breakpoint | the Event Listener Breakpoints pane |
| which address is asked for | a fetch breakpoint | the XHR/fetch Breakpoints pane |
| the name of the function | debug(name) | the Console |
Conditional breakpoints: only when it matters
A loop runs a thousand times, and it goes wrong on one of them. A plain breakpoint stops every time. A conditional breakpoint holds an expression, and pauses only when it is true.
<!DOCTYPE html>
<script>
let sum = 0;
for (let i = 1; i <= 5; i++) {
sum = sum + i;
}
console.log(sum);
</script>
The debugger shows
Paused at: sum = sum + i;
i = 4
sum = 6
The Console shows
Info 15
With the condition i === 4, the script ran through the first three rounds, and stopped once. Without it, Chrome stopped five times.
Logpoints: a log with no change to your file
A logpoint prints to the Console and does not pause, and it lives in DevTools, not in your file. Right-click a line number, choose Add logpoint, and type what console.log would take, such as "i is", i.
<!DOCTYPE html>
<script>
let sum = 0;
for (let i = 1; i <= 5; i++) {
sum = sum + i;
}
console.log(sum);
</script>
The Console shows
Info i is 1
Info i is 2
Info i is 3
Info i is 4
Info i is 5
Info 15
Nothing to remove later, and nothing can be left in by mistake. It is often the best way to log.
Pause on exceptions: stop where it breaks
In the Breakpoints pane are two boxes: Pause on uncaught exceptions, and Pause on caught exceptions. With the first ticked, the script stops on the very line that throws, with every value still in place. By the time you read a red line in the Console, the moment has gone. Here it is still there.
<!DOCTYPE html>
<script>
function read(item) {
return item.price;
}
read(undefined);
</script>
The debugger shows
Paused at: return item.price;
call stack: read, called from (top level)
item = undefined
The Console shows
Error Uncaught TypeError: Cannot read properties of undefined (reading 'price')
DOM change breakpoints: who touched this?
A banner vanishes from a page, and you have no idea which of ten scripts did it. In the Elements panel, right-click the node, choose Break on, then one of three: Subtree modifications, Attribute modifications, or Node removal. The script stops on the very line that makes the change.
<!DOCTYPE html>
<style>
.hidden { display: none; }
</style>
<p class="note">Delivery is free today</p>
<button>Close</button>
<script>
function close() {
document.querySelector(".note").classList.add("hidden");
}
document.querySelector("button").addEventListener("click", close);
</script>
The debugger shows
Paused at: document.querySelector(".note").classList.add("hidden");
call stack: close
Event listener breakpoints: what runs on a click?
You click a button on a large site, and something happens. Which code? In the Debugger, open Event Listener Breakpoints, then Mouse, and tick click. The next click stops at the first line of whatever handles it. You need know nothing about the code.
<!DOCTYPE html>
<button>Add a tea</button>
<p class="out">0</p>
<script>
let count = 0;
function add() {
count = count + 1;
document.querySelector(".out").textContent = count;
}
document.querySelector("button").addEventListener("click", add);
</script>
The debugger shows
Paused at: count = count + 1;
call stack: add
count = 0
Fetch breakpoints: who asked for this?
The Network panel shows a request you do not recognise. In XHR/fetch Breakpoints, click the plus button and type part of its address. The script stops on the line that sends it. The Initiator column can often tell you too, and this works when the request comes from deep inside a library.
On a real site
<!DOCTYPE html>
<script>
function load() {
fetch("stock.json");
}
load();
</script>
The debugger shows
Paused at: fetch("stock.json");
call stack: load, called from (top level)
A memory aid
Start from what you know. The line, the value, the error, the element, the event, the address, or the function: there is a pin for each.
Worked example 4
A cart adds up 200 orders, and the total is a little too high. The loop is fine for most orders. Find the bad order, without stopping 200 times.
What we have: a loop with one bad row
What we need: the row where something unusual is true
The plan: use a conditional breakpoint with a condition that no good order would meet: here, a quantity above any real one
- Right-click the line number inside the loop, and choose Add conditional breakpoint.
- Type
order.qty > 100, and press Enter. Reload. - It stops once. The Scope pane shows the order, with a quantity of 150 typed for 15.
<!DOCTYPE html>
<script>
const orders = [{ item: "Rice", qty: 2 }, { item: "Dal", qty: 150 }, { item: "Tea", qty: 3 }];
let units = 0;
for (const order of orders) {
units = units + order.qty;
}
console.log(units);
</script>
The debugger shows
Paused at: units = units + order.qty;
units = 2
The Console shows
Info 155
The Dal order, 150 for 15. One stop in a long loop, found by a condition.
Check: the debugger stopped once, on the second order, with units at 2.
Worked example 5
A site you have never seen shows a free-delivery note, and something removes it a moment after a click. Which line does it?
What we have: an element that changes, and no knowledge of the code
What we need: the line that changes it
The plan: a DOM change breakpoint starts from the element, so no knowledge of the code is needed
- Inspect the note. In the tree, right-click it, then Break on, then Attribute modifications.
- Click the button. The Sources panel opens at the line that changed it.
- Read the Call Stack to see which function called it.
<!DOCTYPE html>
<style>
.hidden { display: none; }
</style>
<p class="note">Free delivery</p>
<button>Shop</button>
<script>
function tidy() {
const note = document.querySelector(".note");
note.className = "hidden";
}
function onShop() {
tidy();
}
document.querySelector("button").addEventListener("click", onShop);
</script>
The debugger shows
Paused at: note.className = "hidden";
call stack: tidy, called from onShop
The line in tidy, called from onShop, found with no search through the code.
Check: the debugger stopped on that line, with tidy and onShop in the stack.
When you are stuck: choosing a breakpoint
You do not know the line: start from the error, the element, the event or the address.
A conditional breakpoint never fires: try the condition in the Console while paused, or log it.
A pin you set earlier keeps stopping you: untick it in the Breakpoints pane, or use the Deactivate button to switch them all off.
Your turn
You want to read a value in a loop, and not stop. Which kind?Tap to reveal
- It prints, and does not pause.
A logpoint.
An element's class changes, and you do not know who changes it. Which kind?Tap to reveal
- Start from the element.
A DOM change breakpoint, on attribute modifications.
A loop goes wrong only when qty is 0. How do you stop only then?Tap to reveal
- An expression.
A conditional breakpoint: qty === 0.
Did you know?
Logpoints, from Chrome 73
Logpoints came to Chrome in version 73, whose release notes put them first. Their purpose fits in one line: use logpoints to log messages to the Console without cluttering up your code with console.log() calls. You add one by right-clicking a line number. The page's own files stay untouched, so nothing has to be cleaned up afterwards.
Why it fits here: It is the kind of breakpoint to reach for when you only want to read a value, and not stop the page.
Source: Chrome for Developers: What's New In DevTools (Chrome 73)
Watchpoints, hundreds of times slower
Other debuggers have a breakpoint that watches a value instead of a line. GDB calls it a watchpoint, sometimes called a data breakpoint: it stops whenever the value of an expression changes, without having to predict where. Done in software, GDB single-steps the program and tests the value each time, which its manual says is hundreds of times slower than normal execution.
Why it fits here: A DOM change breakpoint works in the same spirit for an element: you say what to watch, and the browser finds the line.
Source: GDB manual: Setting Watchpoints
In one line
Choose a breakpoint by what you know. Use a condition for one case in many, a logpoint to read without stopping, and exceptions for the line that throws. Use DOM changes for who touched an element, events for what ran, and fetch for who asked.
Finding what logs would hide
At a glance
A silent bug has no red line to follow. The way in is always the same: pick a point in the middle, stop there, and compare what you see with what you expected.
Say what the code should do, with one example. Pause just before the answer is decided, and read every value in the Scope pane: quotation marks mean text, and text added to a number joins instead of adding. Try the fix in the Console while paused, then change the file.
Take a guess: In the Scope pane, what do quotation marks around a value tell you?
A silent bug has no red line to follow. The way in is always the same: pick a point in the middle, stop there, and compare what you see with what you expected.
| Step | Do this | Why |
|---|---|---|
| 1 | Say what the code should do, with one example | "7 is a child, so ₹10." You cannot spot wrong without right |
| 2 | Pause just before the answer is decided | a breakpoint, or debugger, on that line |
| 3 | Read every value in the Scope pane | look for a value, a type or an undefined you did not expect |
| 4 | Right so far? Step on. Wrong? Go back to where it was set | the Call Stack shows who passed it |
| 5 | Try the fix while paused, in the Console | no file changes until you know |
| 6 | Change the file, and remove anything you added | the file is the only fix that lasts |
Here is the tram office's bug. A child travels for ₹10 and an adult for ₹20, and a child is anyone under 12. The ages and the limit are read from the page.
<!DOCTYPE html>
<p>Age: <span class="age">11</span></p>
<p>Child up to: <span class="limit">12</span></p>
<p class="out"></p>
<script>
const age = document.querySelector(".age").textContent;
const limit = document.querySelector(".limit").textContent;
debugger;
const isChild = age < limit;
document.querySelector(".out").textContent = isChild ? "₹10" : "₹20";
</script>
Checked in Chrome
- age 11: ₹10, which is right
<!DOCTYPE html>
<p>Age: <span class="age">7</span></p>
<p>Child up to: <span class="limit">12</span></p>
<p class="out"></p>
<script>
const age = document.querySelector(".age").textContent;
const limit = document.querySelector(".limit").textContent;
debugger;
const isChild = age < limit;
document.querySelector(".out").textContent = isChild ? "₹10" : "₹20";
</script>
The debugger shows
Paused at: debugger;
age = "7"
limit = "12"
Checked in Chrome
- (the slip: age 7 pays ₹20, the adult fare)
Eleven works, and seven does not. Nothing is misspelt, there is no error, and a log of age would print 7, which looks perfect. The debugger shows the difference: "7" has quotation marks. Both values came from the page, and page text is text.
Text is compared letter by letter, as in a dictionary. "7" and "12" are compared by their first letters: 7 against 1. Since 7 comes after 1, the text "7" counts as larger. "11" and "12" share a first letter, and 1 comes before 2, so that one gives the right answer by luck.
console.log("7" < "12");
console.log("11" < "12");
console.log(7 < 12);
false
true
true
A memory aid
Quotation marks in the Scope pane mean text. Numbers read from a page, a form or a file are text until you make them numbers.
While still paused, type in the Console to test the fix before touching a file: Number(age) < Number(limit) answers true.
<!DOCTYPE html>
<p>Age: <span class="age">7</span></p>
<p>Child up to: <span class="limit">12</span></p>
<p class="out"></p>
<script>
const age = document.querySelector(".age").textContent;
const limit = document.querySelector(".limit").textContent;
debugger;
const isChild = Number(age) < Number(limit);
document.querySelector(".out").textContent = isChild ? "₹10" : "₹20";
</script>
The debugger shows
Paused at: debugger;
age = "7"
limit = "12"
Checked in Chrome
- with the values made into numbers, age 7 pays ₹10
| Reach for | When |
|---|---|
| a log or a logpoint | you know what to ask, or the code runs many times, or timing matters |
| a breakpoint | you do not know where to look, or you need to see everything at once |
| pause on exceptions | a red line appears, and you want the moment itself |
| a DOM or event breakpoint | you know the effect, and not the code |
Pausing where timing matters
A breakpoint stops the script, not the world. A request may time out, and a menu that closes when it loses focus will close as soon as you click into DevTools. For those, use a logpoint, which does not stop anything. The Styles tab's Emulate a focused page option also keeps such menus open.
Worked example 6
A stock total should add 12 bags and 7 bags, and the page says 127. There is no error. Use the method to find out why.
What we have: a sum that gives 127, not 19
What we need: the types of the two values
The plan: pause before the sum, and read the Scope pane; look for quotation marks
- Expect 19. Pause on the line that adds them.
- The Scope pane reads
rice = "12"anddal = "7". Both in quotation marks: text. - For text, + means join, so 12 and 7 become 127.
- In the Console, while paused, try
Number(rice) + Number(dal): it answers 19.
<!DOCTYPE html>
<p class="rice">12</p>
<p class="dal">7</p>
<p class="out"></p>
<script>
const rice = document.querySelector(".rice").textContent;
const dal = document.querySelector(".dal").textContent;
debugger;
document.querySelector(".out").textContent = rice + dal;
</script>
The debugger shows
Paused at: debugger;
rice = "12"
dal = "7"
Checked in Chrome
- the page shows the wrong 127
<!DOCTYPE html>
<p class="rice">12</p>
<p class="dal">7</p>
<p class="out"></p>
<script>
const rice = Number(document.querySelector(".rice").textContent);
const dal = Number(document.querySelector(".dal").textContent);
document.querySelector(".out").textContent = rice + dal;
</script>
Checked in Chrome
- with numbers, the page shows 19
Both were text, and + joined them. Number() makes them numbers, and the total is 19.
Check: the debugger showed the quotation marks, and the fixed page shows 19.
When you are stuck: silent bugs
Everything in Scope looks right: step on, one line at a time, and say what you expect before each step.
A value is wrong when it arrives: go up the Call Stack to whoever passed it.
It works when you step, and fails when you run: suspect timing; use logpoints.
Your turn
The Scope pane shows price = "60". What kind of value is it?Tap to reveal
- Quotation marks.
Text, not a number.
What is "5" + 2?Tap to reveal
- For text, + joins.
"52".
A menu closes when you click into DevTools. What do you use to study it?Tap to reveal
- Something that does not stop.
A logpoint, or Emulate a focused page.
Did you know?
One sign, two jobs
JavaScript's + does two jobs, and its standard says so in one line: the addition operator either performs string concatenation or numeric addition. The steps that follow give the rule. If either side is a String, both sides are turned into text and joined. So "7" + 3 gives "73", while 7 + 3 gives 10.
Why it fits here: It explains the bug this topic hunts: a price read from the page is text, and + joins it instead of adding.
The value not equal to itself
One wrong value hides well: NaN, a value representing Not-A-Number. It is the only value that compares unequal to itself, so NaN === NaN is false, and a test such as price === NaN can never be true. To tell whether a value is NaN, MDN advises Number.isNaN() or isNaN(), or the self-comparison x !== x.
Why it fits here: In the Scope pane, NaN looks like any other value; reading every value, as this topic does, is how you catch it.
Sources: MDN: NaN; ECMAScript Language Specification
In one line
For a silent bug, say what you expect, pause just before the answer is decided, and read every value: quotation marks mean text. Try the fix in the Console while paused, then change the file.
Where it is used
Developers who use the debugger well solve in minutes what others chase for hours.
| What you learned | Where it is used | A real example |
|---|---|---|
| a line-of-code breakpoint | code you know | reading the values inside a checkout function |
| stepping | following a path through code | seeing which branch of a discount rule runs |
| conditional breakpoints | long loops | stopping on the one order of 10,000 with no price |
| logpoints | code you cannot edit, or that runs often | tracing a scroll handler with no change to the file |
| pause on exceptions | crashes | seeing the data at the moment a page fell over |
| DOM change breakpoints | someone else's code | finding which plugin hides a banner |
| event listener breakpoints | large sites | finding what a click really runs |
| the Console while paused | testing a fix | trying a corrected sum with the live values |
Mistakes and debugging
These are the slips everyone makes in this panel.
1. Leaving a debugger statement in
It does nothing for most visitors, and stops the page dead for anyone with DevTools open. The fix: prefer a breakpoint; if you write the word, search for it before you finish.
2. A breakpoint on code that has already run
You set a marker on a line that runs at load, and nothing happens. The fix: reload.
3. Reading a line before it runs
You pause on const total = a + b;, and total is not there yet. The fix: step over once. A breakpoint stops before its line.
4. Stepping into everything
Pressing F11 on every line takes you deep into libraries. The fix: step over by default, and step into only what you suspect.
5. Forgetting your breakpoints
Yesterday's marker stops today's work, and the page seems to hang, because it is paused. The fix: look for the Paused in debugger banner, and use the Breakpoints pane to clear old markers. They stay until you remove them.
6. Changing the wrong file
Changes you type in the Sources panel are not saved to your project, and vanish at a reload. The fix: make the change in your editor. Chrome's Workspaces feature can link the two, if you want it.
7. Fixing where it hurts
The error is in one function, and the wrong value came from another. Adding a check at the crash hides the true cause. The fix: go up the Call Stack, to where the value went wrong.
The debugger's other uses
The debugger is also a way to learn. Open a page you admire, tick the click event under Event Listener Breakpoints, and click something. You can walk through someone else's code, a line at a time, and see how it works.
Quick card
The lesson on one card.
Every step in the lesson
<!DOCTYPE html>
<p class="age">7</p>
<p class="out"></p>
<script>
function isChild(age) {
return Number(age) < 12;
}
function fare(age) {
const child = isChild(age);
return child ? 10 : 20;
}
const age = document.querySelector(".age").textContent;
const price = fare(age);
document.querySelector(".out").textContent = "₹" + price;
</script>
The debugger shows
Paused at: const price = fare(age);
call stack: (top level)
age = "7"
Paused at: const child = isChild(age);
call stack: fare, called from (top level)
age = "7"
Paused at: return Number(age) < 12;
call stack: isChild, called from fare, called from (top level)
age = "7"
Paused at: return child ? 10 : 20;
call stack: fare, called from (top level)
child = true
Checked in Chrome
- age 7 pays ₹10
| To do this | Do this |
|---|---|
| open a file | Ctrl + P |
| set a breakpoint | click a line number, or write debugger |
| resume | F8 |
| step over | F10 |
| step into | F11 |
| step out | Shift + F11 |
| run up to a line | right-click it, Continue to here |
| stop only sometimes | right-click the number, Add conditional breakpoint |
| print and not stop | right-click the number, Add logpoint |
| stop where it throws | tick Pause on uncaught exceptions |
| stop when an element changes | Elements, right-click, Break on |
| stop on a click | Event Listener Breakpoints, Mouse, click |
| stop on a request | XHR/fetch Breakpoints |
| stop at a function | debug(name) in the Console |
| switch all off | the Deactivate button |
And the rule of the fourth lens: stop listening at the door. Go in, and read the wheels.
The story: One tooth at a time
You push the pin through the line where the fare is decided. The panel stops mid-breath, and every wheel holds still with its number showing. You turn the machine forward, one tooth at a time, and read each wheel as it moves.
The age wheel reads "7", and the limit reads "12". Both have quotation marks round them: they are words, not numbers. Compared as words, 7 comes after 12, as Z comes after A, so the machine had decided that seven was the larger.
Corin stares. "All morning," he says, "and it was two small marks." He takes his notes out, one by one.
Reagents gathered: Sources panel, breakpoint, Step over, Scope, Call Stack, logpoint.
Sources
- Chrome DevTools documentation (Page content CC BY 4.0, code samples Apache 2.0 (the footer of its pages, read 2026-11-06); reused with credit)
- MDN Web Docs (Prose CC BY-SA 2.5 or later; code samples added since 2010-08-20 CC0 (its licensing page, read 2026-10-26); reused with credit)