Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced20 minLESSON 30 / 34

Debugging as a discipline

01

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.

Choose an observation tool for your question
ToolQuestion it helps answerUseful habit
console.logWhat value reached this point?Label the value and relevant input
console.tableHow do a collection’s fields compare?Choose only the fields you need
console.traceWhich calls brought execution here?Use when there is no thrown error yet
console.assertDid an expected condition hold?Remember that a console assertion is not a test runner
Breakpoint / conditional breakpointWhat is the state before the failure?Pause close to the first bad assumption
Pause on exceptionsWhere 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.

02

Mental model

FOLLOW THE VALUE

reported result
form hypothesis
wrong arithmetic

State what should happen for one concrete input.

01 / 03Trace the transformation

03

Live example

The explicit zero check marks an invalid input boundary. For the valid input, inspect the return and predict the result before running.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Test the arithmetic hypothesis

Use the logged input and expected result to correct the operation inside divide.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

count=3
4

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What is a useful first debugging step?
    02Why use a conditional breakpoint?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson