Lesson 6 of 296
How to read an error message and find help
What you will learn
- Why errors are normal and useful
- The parts of an error message
- The three most common JavaScript errors
- A fixed set of steps to follow when something breaks
- How to search for help well
On this page
Errors are normal
Every developer sees errors every day, at every level. An error message is the computer telling you what went wrong and where. Learning to read it calmly is one of the most valuable skills you can have, and interviewers watch how you react when your code fails.
The parts of an error message
Here is a real error from the browser Console:
Uncaught ReferenceError: userName is not defined
at showGreeting (app.js:12:15)
at app.js:20:1| Part | In this example | What it tells you |
|---|---|---|
| Error type | ReferenceError | The kind of problem |
| Message | userName is not defined | What exactly went wrong |
| File | app.js | Which file |
| Line | 12 | Which line in that file |
| Column | 15 | Which character on that line |
| Stack trace | the lines starting with at | The path the code took to get there |
So this error says: in app.js, on line 12, the code used something called userName, but nothing with that name exists.
The three errors you will see most
1. SyntaxError: the code is written wrongly.
The computer cannot even read it. Usually a missing bracket, quote or comma.
console.log("Hello";SyntaxError: missing ) after argument listFix: add the missing ).
2. ReferenceError: you used a name that does not exist.
Usually a spelling mistake or a variable that was never created.
let total = 10;
console.log(totl);ReferenceError: totl is not definedFix: the name is total, not totl.
3. TypeError: you tried to do something a value cannot do.
For example, changing a const, or using something that is empty.
const price = 5;
price = 10;TypeError: Assignment to constant variable.Fix: use let if the value must change.
let user = undefined;
console.log(user.name);TypeError: Cannot read properties of undefined (reading 'name')This is the most common error in JavaScript. It means: you asked for .name on something that is empty. The real question to ask is "why is user empty?"
What to do when something breaks: six steps
- Read the whole message. Do not close it. Read the type and the message slowly.
- Go to the file and line that the error names.
- Look at that line and the lines just above it. The mistake is often one line before.
- Check the simple things first: spelling, capital letters, missing brackets, missing quotes.
- Print values with
console.log()to see what the code really has at that point. - Change one thing at a time and run it again. If you change five things, you will not know which one fixed it.
When there is no error but the result is wrong
Sometimes the code runs but gives the wrong answer. This is a bug in the logic. Finding and fixing bugs is called debugging. Use console.log() to print values at each step until you find the first place where the value is not what you expected.
let price = 10;
let quantity = "3";
console.log(price, quantity); shows: 10 "3"
console.log(price + quantity); shows: "103", not 13Printing the values shows that quantity is text, not a number. That is the cause.
How to search for help
- Copy the error message, but remove the parts that belong only to your project (your file names and variable names).
- Add the technology name. Search for
TypeError Cannot read properties of undefined JavaScript. - Prefer official documentation. For JavaScript, HTML and CSS, the best reference is MDN Web Docs (developer.mozilla.org).
- Check the date of answers you find. Old answers can be out of date.
- Understand the fix before you use it. Do not paste code you cannot explain.
How to ask another person for help
Give them four things:
- what you are trying to do
- what you expected to happen
- what happened in reality, with the full error message
- the smallest piece of code that shows the problem
A note on AI tools
AI tools can explain an error quickly, and using them at work is normal. But in most coding interviews they are not allowed. Practise reading and fixing errors yourself first, then use AI to check your thinking.
Common mistakes
- Not reading the message. Most errors say exactly what is wrong.
- Looking only at the named line. A missing bracket on line 10 can be reported on line 11.
- Fixing only the last error in a long list. Start with the first error. Later errors are often caused by the first one.
- Changing code at random until it works. You learn nothing, and the bug often comes back.
How to say it in an interview
"When I get an error I read the full message first: the type, the message, and the file and line number. I go to that line, check the simple causes, and log the values to confirm what the code has at that point. I change one thing at a time. If I am stuck, I search the message with the technology name and check the official documentation."
Key words
| Word | Meaning |
|---|---|
| Error | A message that says the code could not run correctly |
| Bug | A mistake in code that gives a wrong result |
| Debugging | Finding and fixing bugs |
| Stack trace | The list of steps the code took before the error |
| SyntaxError | The code is written in a way the computer cannot read |
| ReferenceError | A name was used that does not exist |
| TypeError | A value was used in a way it does not support |
| Documentation | The official written guide for a technology |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.