Debugging as a discipline
Understand the idea
The highlighted line is where the program crashed, but the wrong value was created several calls earlier. Replacing that one line hides the cause.
Make one testable prediction
The first error message is a starting point, not a verdict about your ability. Save the smallest input and action that reliably produces it. Write what you expected beside what happened, then choose one observation that could distinguish two possible causes.
Reproduce and reduce the failure
Start with a reliable reproduction: the input, the action, the actual result, and the expected result. Reduce unrelated work until the failure is small enough to explain. A bug that disappears after several simultaneous edits has not taught you which assumption was wrong.
| Tool | Question it helps answer | Useful habit |
|---|---|---|
| console.log | What value reached this point? | Label the value and relevant input |
| console.table | How do a collection’s fields compare? | Choose only the fields you need |
| console.trace | Which calls brought execution here? | Use when there is no thrown error yet |
| console.assert | Did an expected condition hold? | Remember that a console assertion is not a test runner |
| Breakpoint / conditional breakpoint | What is the state before the failure? | Pause close to the first bad assumption |
| Pause on exceptions | Where does a thrown failure begin? | Distinguish handled errors from unexpected ones |
Read the stack trace
Read an error's message and the first relevant application frame in its stack trace. The top frame usually identifies where the error was thrown, while callers explain how execution got there. Async boundaries and bundled code can complicate that view, so use source maps and inspect the original source when available.
A stack trace lists the active calls leading to a failure. Start with the first relevant frame in your code, then follow its callers to see how the bad value arrived. The deepest reported line may be where the problem became visible rather than where it began. Framework frames can be useful, but you do not need to understand every one before inspecting your own input.
Pause and inspect
A breakpoint lets you inspect values before they become invalid. Step over a call to keep your current level of detail; step into it when the caller's inputs look correct but the result does not. Conditional breakpoints and pause-on-exceptions help when a loop or repeated event makes ordinary stepping noisy.
A breakpoint pauses execution at a chosen line. Step over runs the current call without visiting its internals; step into follows the call; step out finishes the current . Inspect the actual values in while paused. A conditional breakpoint can stop only when the suspicious input occurs.
Choose the console tool for the question
The console can organize evidence too. Use console.table for records, console.group for a related sequence, console.trace for callers, and console.assert for a condition that should hold. Logged may be inspected later in a changed state, so record the specific fields or a suitable snapshot when timing matters. Finish with a regression check that fails before the repair and passes afterward.
In the example, a count of zero is rejected explicitly. Change the input to reproduce that failure, then read the message and caller path. When you restore a valid count, check the original behavior again. A fix should explain the observation and survive the case that exposed it, not merely make the red text disappear.
Mental model
FOLLOW THE VALUE
State what should happen for one concrete input.
01 / 03Trace the transformation
Live example
The explicit zero check marks an invalid input boundary. For the valid input, inspect the return and predict the result before running.
Your console output appears here.
Guided challenge
Test the arithmetic hypothesis
Use the logged input and expected result to correct the operation inside divide.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
count=3 4
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.