The detective's method
Seven steps of debugging, reading errors and stack traces, labeled console.log, debugger and the rubber duck.
Byte's method has seven steps:
- Reproduce. Make the bug happen for you, step by step. No reproduction — nothing to fix.
- Read the evidence. The error message and the stack trace aren't noise; they're a witness statement.
- Form a hypothesis. "I think
priceis a string here." - Check it. Look at the real value:
console.log, the debugger. - Narrow it down. Rule out half of the suspects, then half again.
- Fix the cause, not the place where it crashed.
- Lock it in with a test, so the bug never comes back.
const order = { id: 42, items: ["Cat food"] };
try {
console.log(order.customer.name);
} catch (error) {
console.log("Type:", error.name);
console.log("Message:", error.message);
}The message Cannot read properties of undefined (reading 'name') doesn't say "there's no name". It says "the thing to the left of the dot is empty". To the left is order.customer, and it's undefined. So the question isn't about name — it's about where the customer went. (Firefox says the same thing as order.customer is undefined — same meaning.)
// This is what a crash looks like in the Chrome console:
//
// Uncaught TypeError: Cannot read properties of undefined (reading 'title')
// at renderCard (feed.js:24:31)
// at Array.map (<anonymous>)
// at renderFeed (feed.js:9:22)
// at onLoad (app.js:41:3)Read the stack trace from top to bottom:
- the first line is the type and the message;
at renderCard (feed.js:24:31)— it crashed in the functionrenderCard, filefeed.js, line 24, column 31;Array.mapis a built-in method, skip it: the bug isn't in JavaScript;renderFeed (feed.js:9:22)— this is the one that calledrenderCardthrough map, and apparently passed it undefined instead of a post. The investigation's next stop: line 9.
A glossary of common evidence:
x is not defined(ReferenceError) — there's no variable with that name: a typo, or you forgot to declare or import it;x is not a function— you called something that isn't a function: a typo in a method name, a method on the wrong object;Unexpected token(SyntaxError) — the code couldn't even be parsed: a missing bracket or quote. Not a single line ran;NaNinstead of a number — no error at all: an undefined or a string got into the arithmetic somewhere;- "one item too many" — also no error: the classic off-by-one,
<=instead of<.
function total(prices) {
let sum = 0;
for (let i = 1; i < prices.length; i++) {
console.log("i =", i, "price =", prices[i], "sum =", sum);
sum += prices[i];
}
return sum;
}
console.log("total:", total([100, 250, 40]));i = 1.The console helps you check hypotheses:
console.log("label:", value)— always with a label, or in a minute you won't know which number means what;console.table(rows)— an array of objects as a table;console.group("order")… console.groupEnd() — fold output into a collapsible group;- console.error — a red line with a stack trace.
And there's the debugger. The line debugger;, or a click on a line number in the Sources tab (that's a breakpoint), pauses the program if DevTools (F12) are open. While paused you see every variable, and you can run the code line by line. The Tasky sandbox has no DevTools, so try it in your own browser.
If you don't even know where to look, narrow it down by halves. Comment out half of the suspicious code: the bug is still there — it's in the other half. Repeat. Ten halvings and a thousand lines become one. The same works on history: the git bisect command splits commits in half by itself and finds the one that broke everything (we'll get to git later, and in this chapter's lab you'll write that search yourself).
Pop quiz
Mistakes are fine: keep trying until you find the right answer, and Byte explains why it's right.