JavaScript Errors
A JavaScript error is an object the engine creates when something goes wrong while your code is being read or run, such as using a variable that does not exist or calling something that is not a function. Errors stop the current script, print a message in the console, and, once you understand their types, tell you exactly where to look.
Reading an error message
Every error has a name, a message and usually a stack trace. In the console, you might see:
Uncaught TypeError: Cannot read properties of undefined (reading 'name')
at showUser (app.js:14:22)
at app.js:20:1
Read it from left to right. TypeError is the kind of problem. The message says something was undefined when you tried to read .name from it. The stack trace lists where it happened: inside showUser, in app.js, line 14, column 22, which was called from line 20. Click the file link in developer tools to jump straight there.
The built-in error types
| Type | When it happens | Example cause |
|---|---|---|
SyntaxError | Code cannot be parsed | Missing bracket, or JSON.parse('{bad}') |
ReferenceError | Using a name that is not defined | Typo like consol.log, or reading a let before its line |
TypeError | A value is the wrong type for the operation | Calling undefined() or reading a property of null |
RangeError | A number is outside the allowed range | new Array(-1), or endless recursion |
URIError | Bad input to URI functions | decodeURIComponent('%') |
All of them inherit from the general Error type, which you can also use yourself.
Syntax errors versus runtime errors
A syntax error is found before the script runs, so none of that file's code executes. That is why a single missing brace can make an entire page "do nothing". Runtime errors, like TypeError, happen while the code is running; everything before that line already ran, and the rest of that call stops.
Throwing your own errors
You can signal problems in your own functions with throw. Always throw an Error object rather than a plain string, because only error objects carry a stack trace that shows where the problem started.
function withdraw(balance, amount) {
if (typeof amount !== 'number' || amount <= 0) {
throw new TypeError('Amount must be a positive number');
}
if (amount > balance) {
throw new RangeError('Insufficient balance');
}
return balance - amount;
}
try {
withdraw(500, 800);
} catch (err) {
console.log(err.name); // "RangeError"
console.log(err.message); // "Insufficient balance"
}
The try and catch blocks here are a preview; the next lesson explains error handling mechanisms properly.
The most common error and how to prevent it
"Cannot read properties of undefined" is probably the error beginners meet most. It usually means data was not there when you expected it: an API response with a different shape, an array index past the end, or an element that querySelector did not find. Log the value just before the failing line to see what it really is. Optional chaining (user?.name) helps when a value is legitimately allowed to be missing, but do not use it to hide bugs where the value should always exist.
Habits that make errors easier
- Keep the developer tools console open while you work, so you see errors the moment they happen.
- Fix the first error in the list first; later ones are often side effects of it.
- Search the exact message text when you do not recognise it.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.