Lesson 25 of 28
In progress
Debugging with the browser tools
Use breakpoints, the call stack and the network panel to find a bug, and narrow the problem instead of changing code at random.
Debugging is a method, not a talent. Reproduce the problem, narrow where it happens, inspect the actual values, then change one thing. The browser tools make each step faster.
What you will learn
- Pause code with breakpoints and read the call stack.
- Inspect values without scattering logs.
- Use the network panel to separate code bugs from request bugs.
Reproduce first
A bug you cannot reproduce is a bug you cannot verify you fixed. Write down the exact steps: which page, which input, which click. If it only happens with a specific value, keep that value.
Then reduce it. Remove half the steps, then half the remaining ones, until the smallest sequence that still fails is clear. That sequence usually points at the component responsible.
Breakpoints
Open DevTools, go to Sources, find your file and click a line number. The next time execution reaches it, the page pauses and everything in scope is visible.
- Step over runs the current line.
- Step into enters the function being called.
- Step out finishes the current function.
- Resume continues to the next breakpoint.
A conditional breakpoint pauses only when an expression is true, such as item.id === 42. That is how you debug a loop without stopping on every iteration.
debugger; in the source does the same when DevTools is open. Remove it before shipping; a paused page in production is a broken page.
Read the scope and the stack
When paused, the Scope panel lists local, closure and global variables. The Call stack shows how you got there: the current function, who called it, and so on.
That stack answers the question logs cannot: which caller passed the wrong value. Click a frame to inspect that function’s variables at the moment of the call.
console.trace() prints a stack trace on demand, which is useful in code you cannot easily pause, such as a callback that runs on a timer.
Logs worth keeping
Editable. Runs in your browser.
console.table(items);
console.log({ items, count: items.length });
console.group("Cart");
console.log("items", items.length);
console.groupEnd();
Press Run to see the output.
console.table is easier to scan than nested objects. Wrapping values in an object labels them without string concatenation. console.group nests related output, and console.time/timeEnd measures a section.
Do not leave labelled debug output in shipped code. It clutters the console and can leak data.
Network problems are different
If a feature depends on a request, check the Network panel before touching the code:
- Was the request made at all?
- What status came back: 200, 404, 500, or a blocked CORS request?
- What did the response body contain?
A 404 means the URL is wrong. A 500 means the server failed and the client cannot fix it. A CORS error means the response headers are wrong. Each has a different fix, and the console message alone rarely distinguishes them.
Also check the request payload. A form submitted with the wrong field name looks like a server bug and is a client typo.
Common browser-specific traps
- The element is
nullbecause the script ran before the DOM was parsed. Usedefer. - The event handler runs twice because the script was included twice.
- A click does nothing because an overlay covers the button; the Elements panel shows what is on top.
- Cached files hide a change; hard-reload and check the status is 200, not 304.
- A CSS rule is overridden; the Styles panel shows the winner and the loser.
Change one thing
The temptation is to change three things and hope. Change one, retest the same reproduction, and note the result. If it did not help, revert before trying the next idea, so you always know what is currently true.
Adding a log inside a loop and reading a hundred identical lines tells you the loop runs, not why the value is wrong. Use a conditional breakpoint or log the specific value with a condition, so the output answers a question you actually asked.
This function should return the total of prices above 100, but returns 0. Describe how you would find the cause with a breakpoint, without adding logs.
function bigTotal(prices) {
let total = 0;
for (const price of prices) {
if (price > 100) total += price;
}
return total;
}
Show solution
Set a breakpoint on the if line, call the function with a known array such as [50, 150], and inspect price and total in the Scope panel on each pause. If price is a string, price > 100 compares as text and may be false, which explains the zero. Fix by converting at the boundary, then rerun the same call and step through once to confirm the total changes.
The point is that the breakpoint answers "what is the actual value here" in one pause, rather than ten logs that only prove the loop runs.
Recap
- Reproduce and reduce before changing anything.
- Breakpoints, the Scope panel and the call stack show values and callers that logs cannot.
- Check the Network panel before blaming your code for a request failure.
Next, you will write a first automated test so a fixed bug stays fixed.
Why does a breakpoint never hit?
Should I keep console.log in production?
Progress is saved in this browser only. No account, no email.