Skip to the content.

Lesson 10: Debug with AI

Course home Previous: Lesson 9 Next: Lesson 11

Time: about 60 minutes.

Goals

By the end of this lesson you will be able to:

Concepts

DevTools tour

Open DevTools with F12, or Cmd+Option+I on macOS. The Chrome DevTools docs and MDN’s JavaScript debugging guide cover everything; you need four panels:

Panel Use it to
Console See errors (red) and warnings; run quick JavaScript
Elements (Inspector in Firefox) See the live HTML and CSS; check what is actually on the page
Application (Storage in Firefox) View and edit localStorage
Sources (Debugger in Firefox) Pause code on a line with a breakpoint and inspect values

Reading an error

Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
    at app.js:121:6

Translation: document.querySelector(...) found nothing (returned null), probably because the id in JavaScript does not match the id in HTML.

Common beginner errors:

Error Usual meaning
Cannot read properties of null / undefined An element or value you expected does not exist
X is not a function A typo, a missing import, or the thing is not what you think
X is not defined A variable or function was never declared or imported
Unexpected token A syntax mistake: a missing bracket, comma or quote
Failed to load module script / CORS errors on file:// You opened the HTML directly instead of through a local server
The requested module does not provide an export named X Import name does not match the export

The debugging loop

  1. Reproduce: find exact steps that trigger the bug every time.
  2. Isolate: narrow it down. Which file? Which function? Which input?
  3. Explain: state the cause in one sentence before changing anything.
  4. Fix: the smallest change that addresses the cause.
  5. Prevent: add a test that would have caught it.

Agents are good at steps 2 to 4 if you give them step 1. Without exact steps and error text, they guess.

A bug report agents can act on

Bug: <one-line summary>
Steps:
1. ...
2. ...
Expected: ...
Actual: ...
Console: <exact error text, copied, not paraphrased>
Already tried: ...
Please find the cause and explain it before changing anything.

When to stop

If the agent has tried two fixes and the bug is still there:

  1. Stop. Do not say “try again”.
  2. git restore . to get back to the last working commit.
  3. Start a fresh session with the full bug report and “explain the cause first”.
  4. If that fails, isolate it yourself with a breakpoint or a smaller test case.

Repeated retries in a long session tend to pile changes on top of a misunderstanding.

Walkthrough

You will plant bugs in your own app on purpose and then debug them. Commit your work first so you can always get back:

git status   # should be clean

Bug 1: the id mismatch

In index.html, change id="habit-list" to id="habits-list". Reload the app with the Console open.

  1. Reproduce: just loading the page. Read the red error.
  2. Isolate: click the app.js:NN link in the error. DevTools opens the line.
  3. Explain: in one sentence, what is wrong? Write it down before asking the agent.
  4. Now ask the agent with a proper report:
Bug: the page shows no habits and the form does nothing.
Steps: 1) start the server 2) open http://localhost:8000
Expected: the app works as before.
Actual: nothing happens when I press Add.
Console: [paste the exact error and the line number]
Find the cause and explain it before changing anything.

Compare its explanation with yours. Then git restore index.html to undo your planted bug.

Bug 2: the logic bug that throws no error

In habits.js, in currentStreak, change addDays(day, -1) inside the loop to addDays(day, 1). Save and reload.

No error appears. The streak just looks wrong. This is the common kind of bug: silent.

  1. Run node --test. Which tests fail? The failure output tells you expected vs actual.
  2. Use a breakpoint: in DevTools Sources, open habits.js, click the line number inside the while loop, tick a habit. Execution pauses. Hover over day and streak. Press the “step over” button a few times and watch day move the wrong way.
  3. Ask the agent, giving it the failing test output:
node --test shows this failure: [paste]. Find the cause in habits.js and
explain it in one sentence. Do not change the test.

Restore: git restore habits.js.

Bug 3: the data bug

In DevTools Application, Local storage, change your saved value so that one habit’s done list contains "2026-13-45". Reload.

Bug 4: the “works on my machine” bug

Open the app by double-clicking index.html (a file:// URL) instead of using the server. The app does not start. The Console shows a module or CORS error.

Ask the agent:

When I open index.html by double-clicking it, the console shows: [paste].
When I use python3 -m http.server it works. Explain why in plain English.
Do not change any files.

Good answer: browsers apply security rules to JavaScript modules and refuse to load them from file:// pages; a local server serves them over http:// so they load normally.

Bug 5: a real one, if you have it

By now you have probably hit a real bug in your own version. Write a full bug report for it using the template, run it through the loop, and finish with a test that would have caught it. Commit the fix and the test together:

git commit -am "Fix <bug> and add regression test"

What good output looks like

Checklist

Mini-quiz

  1. What does Cannot read properties of null (reading 'addEventListener') usually mean in a small web app?
  2. What are the five steps of the debugging loop?
  3. Why paste the exact error text rather than describing it?
  4. A bug produces no error at all. What two tools from this course help most?
  5. The agent has tried three fixes and the bug remains. What do you do?
Answers 1. A `querySelector` (or similar) returned `null`: the element was not found, often because of an id mismatch between HTML and JavaScript, or the script ran before the element existed. 2. Reproduce, isolate, explain, fix, prevent. 3. The exact text includes the error type, the precise message and the file and line, which point straight to the cause. A paraphrase loses those details. 4. Automated tests (to show expected versus actual) and breakpoints in DevTools (to watch values change step by step). 5. Stop, revert to the last working commit with Git, start a fresh session with a full bug report asking for the cause first, and isolate it yourself if needed.

Assignment

  1. Ask the agent to plant one subtle bug in a copy of your project without telling you where (git stash or a new branch makes this easy to undo; ask the agent how). Find it using only DevTools and tests.
  2. Write a one-page “Debugging cheatsheet” for your future self in your project’s README: your favourite error explanations and your bug report template.

Next: Lesson 11: Stay safe