Read the error before you read the code
Turn a vague failure report into a reproduction, isolate the cause, form and falsify hypotheses, instrument the code, fix async and state bugs, lock the fix behind a regression test and review changes for real trade-offs.
What you will be able to do
#- Identify an error by its type, message and stack.
- Find the first stack frame that belongs to your own code.
Overview
#An error message is structured data, not noise. It has a type, a message and a stack. Read them in that order.
The type tells you the category. TypeError means a value had the wrong type at the moment it was used. ReferenceError means a name was never declared. RangeError means a value left its allowed range. SyntaxError means the code could not be parsed at all.
A very common message is: Cannot read properties of undefined (reading name). It carries three facts: something is undefined, you tried to read name from it, and the read happened at the top frame of the stack.
function renderUser(user) {
return user.profile.name;
}
If profile is missing, this throws. The fix depends on the contract: is a missing profile a programming error or a valid state?
function renderUser(user) {
const name = user?.profile?.name;
return name ?? 'Anonymous';
}
Optional chaining with a fallback is correct when a missing profile is valid. If it is not valid, throwing early with a clear message is better than silently rendering Anonymous.
The stack is the second half of the story. The top frame is where the error was thrown, but the useful frame is often the first one in your own code, not in a library. Read outward until you reach a file you own. When the code is bundled or minified, a source map maps the frame back to the original line.
The cause property carries the underlying error when you wrap one. Preserve it so the chain is not lost.
try {
await save();
} catch (error) {
throw new Error('Could not save the draft', { cause: error });
}
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Debug
renderUser throws Cannot read properties of undefined (reading name) when a user has no profile. Fix it so a missing profile renders as Anonymous, and keep a real profile unchanged.
starter.javascriptfunction renderUser(user) { return user.profile.name; }Reveal the reference answer
reference.javascriptfunction renderUser(user) { const name = user?.profile?.name; return name ?? 'Anonymous'; }Authored expected output: renderUser({ id: 1 }) returns 'Anonymous'; renderUser({ profile: { name: 'Ada' } }) returns 'Ada'.
The message names the exact read that failed. Optional chaining stops the read when profile is absent, and the nullish fallback supplies a valid display value.
Exercise 2Predict
Which error type is thrown by reading a property from null?
Common misconceptions
#- The top stack frame is always the line you must fix.
- Optional chaining is always the right fix for a missing value.
Follow-up practice
#- debugging-review-03-isolate-by-bisection
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.