How to fix common javascript programming mistakes

Start at the first red line in the console, not the last. JavaScript usually reports one genuine failure and then produces a cascade of follow-on noise from code that ran with broken values. The first message is the one that tells the truth, and it almost always includes a file name and a line number.

Start from the first error

Read the message, then open the file at the line it names. Look at that line and the one directly above it, because a missing bracket or a variable that was never assigned usually breaks the statement that follows. If the line looks correct, work backwards one statement at a time until the values stop making sense. That habit replaces most guesswork.

A loop that finds most problems

  • Reproduce it deliberately: write down the exact clicks and inputs that trigger the bug.
  • Shrink it to the smallest version — one page, one function, hard-coded values.
  • Check the values at the boundary: what went in, what came out, what type each one is.
  • Change one thing, then test again. Two changes at once and you learn nothing.
  • Write down the cause in one sentence before replacing the fix.

That last step is the one people skip. “The array was empty because the fetch had not finished” is a lesson; an edit that happens to make the page work is a coincidence you will meet again next week.

The errors you will actually meet

Cannot read properties of undefined means you are reaching into something that has no value yet — a missing selector result, a response that failed, a variable declared but never set. Log the object itself rather than its property, and the shape of the problem becomes obvious.

Is not a function usually means the name resolves to something else: a value instead of a function, a typo, or a method called on the wrong type. Check what the name actually holds before assuming the call is wrong.

Unexpected token is a syntax problem near the reported position, often one line earlier than the arrow points. A missing comma in an object, an unbalanced bracket, or a stray character from a copy-paste will all do it.

NaN appears when arithmetic meets a string that cannot be parsed. It spreads silently through every calculation that follows, so find the first place it appears rather than the last place it is printed.

When the bug is timing

Some bugs only appear on the first load, or only when the network is slow, and that pattern points at ordering rather than logic. Anything that depends on a response must wait for it: inside a callback, in a promise chain, or after await. If a value is populated on the second click but not the first, you are almost certainly reading it before the work finished.

Patch, isolate, or rewrite

Patch when the bug is local and you understand the cause. Isolate when you cannot reproduce it in a small file — copy the failing behaviour into a fresh page and delete until it disappears. Rewrite a function only when you can describe what it should do more clearly than you can describe what it currently does. Rewriting out of frustration tends to reintroduce the same defect under a new name.

Most of the frustration in the first months is not caused by hard problems. It is caused by fixing without understanding, and the loop above is simply a way of forcing one before the other.