Debugging Checklist

Follow the same order every time: reproduce (write down the steps, what you expected, what happened), read the Console, check Network (status and the server's…

Most bugs take a long time not because they're hard, but because people hunt them by jumping around: change a line, refresh, suspect another file, refresh again. An hour passes, and you're no longer sure what you've already checked.

A debugging checklist stops that. It's an order of steps that stays the same every time you meet a bug. A good mechanic works the same way. They don't take apart the engine of a motorbike that won't run. They start the bike to feel the symptom, then check the fuel, the spark plug and the battery one by one, starting with the easiest to check, until they find the cause. After the fix, they take it for one more test ride. In debugging, starting the bike is making the bug happen again, checking the parts one by one is the steps of the checklist, and the test ride is making sure the bug is gone.

The bug we chase

In the Lab, the Pay button shows Total: $10510, when it should be $115 (a Book at $45 x 2, a Pencil at $5 x 3, shipping $10). This is the code. shipping (the shipping cost) comes from the server through fetch:

const cart = [
  { name: "Book", price: 45, qty: 2 },
  { name: "Pencil", price: 5, qty: 3 },
];

async function calculatePayment() {
  const subtotal = cart.reduce((sum, item) => sum + item.price * item.qty, 0);
  const res = await fetch("/api/shipping?city=bristol");
  const { shipping } = await res.json();
  return subtotal + shipping;
}

Let's follow the checklist from the first step.

Steps 1 to 3: reproduce, Console, Network

  1. Reproduce. Write down the exact steps, what you expected, and what happened. Here: open the cart, click Pay. Expected $115, got $10510, and it happens every time. A bug you can make happen again whenever you want is far easier to hunt. If the bug only happens sometimes, note the conditions: browser, screen size, the logged-in user, particular data.
  2. Read the Console. Is there a red error? If so, read its type, message and stack trace (the Reading Error Messages lesson). In this bug the Console is clean. That's a clue too: the code doesn't stop with an error, so the problem is in the logic or the data.
  3. Check Network. Filter Fetch/XHR, click the request, then check its status code and the Response tab. Here GET /api/shipping?city=bristol has status 200, and its answer is {"shipping":"10"}. Look at the quote marks: the shipping cost is sent as text, not a number.

Step 4: check the value and its type

Don't just print the value; print its type too. console.log(shipping) prints 10, which looks exactly like a number. In the exercise you write a small helper function called debugValue that tells them apart:

debugValue("subtotal", subtotal); // [subtotal] type:number val:105
debugValue("shipping", shipping); // [shipping] type:string val:10

Here is the cause: shipping is a string. In JavaScript, + with a string on either side isn't addition, it's joining text: 105 + "10" becomes "10510". So the result isn't 115. That's the strange number on the Pay button.

Other ways to do this step: console.log({ shipping }) prints the name and value together (a string shows with quote marks). You can also set a breakpoint and read Scope (the Breakpoints & Stepping lesson).

Steps 5 to 7: isolate, fix, lock it in with a test

  1. Isolate. Make the problem smaller until only its core is left. One line, console.log(105 + "10"), already shows the same bug, with no cart and no fetch. If you don't know which part is wrong yet, turn half the code into comments or remove half the data, then check whether the bug is still there.
  2. Fix it at the source. Turn the text into a number before adding: return subtotal + Number(shipping);. The result is 115. Better still, ask the API to send shipping as a number.
  3. Lock it in with a regression test. A regression test is a test (code that checks your code's results automatically) that fails if the same bug comes back. This is the step most often skipped. Yet without a test, a small change next month can quietly bring the same bug back.

Here is the simplest kind of check, with console.assert:

function calculateTotal(subtotal, shipping) {
  return subtotal + Number(shipping);
}

console.assert(calculateTotal(105, "10") === 115, "a string shipping cost must be added as a number");

console.assert(condition, message) stays quiet when the condition is true, and prints Assertion failed: message when it's false. But console.assert only prints. The program still ends with exit code 0 (the number a program reports when it ends: 0 means success), so CI stays green (passing). CI is a server that runs your tests automatically every time code is sent. For a regression test that really fails, write the same check with assert.strictEqual inside test() from node:test, or expect(...).toBe(115) in Vitest. node:test and Vitest are tools for running tests. When that check fails, the test command ends with exit code 1.

Still stuck: the rubber duck and asking for help

Exercise: the debugValue helper

In the exercise you write debugValue(label, value). This function prints a description of the value with its type, then returns value unchanged. This is what each call prints:

Call What gets printed
debugValue("user", null) [user] NULL
debugValue("config", undefined) [config] UNDEFINED
debugValue("items", [1, 2, 3]) [items] Array(3)
debugValue("data", { a: 1, b: 2, c: 3 }) [data] Object{3}
debugValue("count", 42) [count] type:number val:42
debugValue("name", "Budi") [name] type:string val:Budi

The order of the checks decides the result:

🎯 A short decision tree

If you don't know where to start, follow these questions from the top:

A red error in the Console?
  yes -> read its type, its message and the top line of the stack trace
  no  -> check Network: a failed request (4xx/5xx) or an odd response?
         yes -> check its URL, method, headers and body
         no  -> right data but wrong display? check Elements and the CSS
                wrong data? check values and types, trace them back to the source
The bug only happens sometimes?
  -> suspect async order (a forgotten await), the cache (hard reload), or particular data

⚠️ The steps people skip most

TL;DR: Follow the same order every time: reproduce (write down the steps, what you expected, what happened), read the Console, check Network (status and the server's answer), check values together with their types, make the problem as small as you can, fix it at the source, then lock it in with a regression test (a test that fails if the same bug comes back). In this lesson's example, the Console is clean (no errors), Network shows "shipping":"10" as a string, and 105 + "10" joins text into "10510". Number(shipping) fixes it to 115. When stuck, explain your code to a rubber duck (rubber duck debugging). In the exercise, the helper function debugValue prints [label] NULL, [label] UNDEFINED, [label] Array(N), [label] Object{N} or [label] type:X val:Y, checks null and arrays before objects, and returns the value.

⚡ The same order every time

Meaning: Reproduce the bug (make it happen again with the same steps), read the Console, check Network, check values together with their types, isolate it (make the problem smaller until only its core is left), fix it at the source, then lock it in with a regression test (a test that fails if the bug comes back). Like a mechanic who starts the bike first to see the symptom, then checks the fuel, the spark plug and the battery one by one.

⚡ A clean Console isn't proof

Write:

debugValue("subtotal", subtotal); // [subtotal] type:number val:105
debugValue("shipping", shipping); // [shipping] type:string val:10
Guess first: debugValue prints a value with its type. The cart's subtotal is a number, but the shipping cost from the server is a string. What does 105 + "10" give?

"10510". With a string on either side of +, JavaScript joins text instead of adding. That's why the Lab's Pay button shows $10510, not $115.

Meaning: Print the type too, not just the value: 10 and "10" look the same in a plain console.log. Fix it at the source: Number(shipping).

⚡ Lock it in with a regression test

Write:

console.assert(calculateTotal(105, "10") === 115, "a string shipping cost must be added as a number");

Meaning: calculateTotal adds the subtotal and the shipping cost. This line prints Assertion failed if the same bug comes back, but the program still ends with exit code 0 (the sign of success). For CI (a server that runs tests automatically) to fail too, write the same check with node:test or Vitest (tools for running tests). It's the step most often skipped, yet without a test, a small change next month can quietly bring the same bug back.

⚡ Still stuck? The rubber duck

Meaning: Explain your code line by line, out loud, to a rubber duck or to anyone (rubber duck debugging). While explaining, you're forced to check every assumption again, and a bug you couldn't see often shows itself.

What you will learn