‹ Start From Zero Lesson 12 of 21
Contents Lesson 12 of 21

3 min read · foundations

When it breaks — reading errors, and asking well

You will spend more time on things not working than on things working. That is not a beginner's condition; it is the job. This lesson makes it survivable.

Read the error from the bottom

A stack trace is a list of calls, most recent last. The bottom lines are your code; the top may be someone else's. Start at the last line that names a file you wrote.

Three you will meet constantly:

  • is not defined — a typo in a name, or you used it before declaring it.
  • Cannot read properties of undefined (reading 'close') — you asked for .close on something that is not there. Almost always an array index past the end, or a field that was named differently than you assumed.
  • Unexpected token < in JSON — you called .json() on something that is not JSON. Usually an error page, which means the request failed and nothing checked.

Each of those points at a specific mistake, and you have met all three already in this course.

Narrow it before you fix it

The instinct is to change things. The faster move is to find out where it goes wrong:

  1. console.log just above the failing line. Is the value what you expected?
  2. If not, move up and log again. Keep going until the value is right.
  3. The line between "right" and "wrong" is your bug.

This is bisection, and it turns "it doesn't work" into a specific question in about four minutes.

Asking an assistant well

Now that you have a specific question, the assistant is genuinely good. What makes the difference:

  • Paste the actual error, all of it, not a description of it.
  • Paste the code around it, not the whole file.
  • Say what you expected and what happened instead.
  • Say what you already tried.

"It doesn't work" gets you guesses. The four things above usually get you the answer, because they are the same four things a human colleague would ask for.

Read the answer sceptically

It will sound confident either way. Two questions before you accept anything: does this explanation account for the actual error text, and can I say in one sentence why this fixes it?

If you cannot answer the second, you have not fixed the bug — you have changed something and the symptom moved. Course 1 turns this into a rule you will use on every generated change: never commit what you did not understand.

Try it now

Break your fetch script deliberately — call .json() on a 404 response — and practise the whole loop: read the error, bisect to the line, then write the question you would ask an assistant, with all four parts. Writing it out is usually where you find the answer yourself.