The parts of an error
A JavaScript error message has two parts worth reading:
- The type, such as
SyntaxErrororTypeError. It tells you what kind of problem it is. - The message, such as
x is not defined. It names the thing that went wrong.
Read both before touching the code. The wording differs a little between browsers; MDN keeps an error reference with every message and its usual causes.
SyntaxError
The code is not valid JavaScript, so none of it runs. Usual causes are a missing closing bracket, parenthesis or quote. For example, a function call with no closing parenthesis gives missing ) after argument list.
console.log("total:", total; // SyntaxError
console.log("total:", total); // fixedFix: look at the spot the message points to and just before it. Count your brackets: every (, { and [ needs a partner.
ReferenceError: x is not defined
MDN describes this as referencing a variable that does not exist. In practice it is almost always one of these:
- A typo:
totlinstead oftotal. - Wrong case: names are case-sensitive, so
Totalis a different name. - A variable used before you declared it with
letorconst. - A variable declared inside a block and used outside it.
TypeError
The code is valid, but a value is the wrong kind for what you did with it. MDN files these under an unexpected type. Two you will meet early:
Cannot read properties of undefined
You wrote something like items[5].name and items[5] does not exist, so it is undefined. Check the index (arrays start at 0, and the last index is length - 1) and that the variable was set.
x is not a function
You called something that is not actually a function. A classic is "hello".reverse(): arrays have a reverse method, strings do not. Turn the string into an array first: text.split("").reverse().join("").
When there is no error but the answer is wrong
The hardest bugs give no message at all. Some common ones in beginner exercises:
- undefined: a missing
return. - NaN: arithmetic on something that is not a number, often
undefined + 1when a counter was never set. - "23" instead of 5: adding strings joins them.
- Negative remainders: the
%operator takes the sign of the dividend, so-1 % 2is-1, not1. Check for=== 0rather than=== 1.
Reading the line number
In a browser console, an error comes with a location such as script.js:12:5: file, line 12, column 5. Go to that line first. For a SyntaxError the real mistake is often just before the spot named, because the parser only notices something is wrong when the next piece of code does not fit. For a TypeError or ReferenceError the line is usually exactly where the bad value was used, and the question becomes where that value came from.
A worked example
function countVowels(text) {
let count;
for (const ch of text.toLowerCase()) {
if ("aeiou".includes(ch)) count++;
}
return count;
}
countVowels("Hello"); // NaNNo error appears, but the answer is NaN. Trace it: count starts as undefined, and undefined + 1 is NaN. Every later step keeps it NaN. The fix is let count = 0;. When a number comes out as NaN, look for a variable that was never given a starting value.
Habits that help
- Run your code often, after every few lines, so a new error has few suspects.
- Change one thing at a time, then run again.
- Print values with
console.logto see what a variable really holds. - Fix the first error first. Later ones are often caused by it.
Key points
- Read the error type and the message before touching the code.
- SyntaxError: the code is not valid, so nothing ran. Check brackets and quotes.
- ReferenceError: a name does not exist. Check spelling, case and declarations.
- TypeError: a value is the wrong kind. Find where it came from.
- No error but a wrong answer: look for a missing return, a counter without a start value, or strings being added.
- Fix the first error first, run again, and repeat. One change at a time keeps each new error easy to explain, and after a few weeks the common messages will read like plain sentences.
Try it on a real exercise
Write a function, run the tests, get a hint if it fails.
Sources checked 4 December 2026.