JavaScript ES2023 ·
✓ verified by execution on 2026-08-11
When your JavaScript code breaks, the environment (like the browser or Node.js) generates an Error.
Reading an error message is the single most important skill you can learn as a programmer. It separates people who continue coding from people who quit. Let’s learn the three most common error types by causing them deliberately.
SyntaxError: The Parse-Time Failure
Before JavaScript executes your code, it parses (reads) it to understand the structure. A SyntaxError happens when the parser encounters invalid code format.
[!WARNING]
Because SyntaxErrors usually occur during the parsing phase, they happen before execution begins. This means a standard try/catch block cannot catch a syntax error in your regular script. The code simply won’t run at all. (We used JSON.parse above because it parses strings at runtime, which allows us to catch it).
TypeError: The Wrong Value
A TypeError happens when you try to do something with a value that is of the wrong type. For example, trying to call a number as if it were a function, or using a string method on a number.
javascript
try { let num = 42; num.toUpperCase();} catch (e) { console.log(e.name);}
Output
TypeError
Your output
[!TIP]
The most common TypeError you will see in your career is Cannot read properties of undefined. This happens when you try to access a property on a variable that doesn’t hold an object.
ReferenceError: The Missing Variable
A ReferenceError occurs when you try to use a variable that hasn’t been defined in the current scope. JavaScript is telling you, “I don’t know what this name refers to.”
Predict the outputjavascript
Read the code. What exactly will it print? Commit to an answer before you look.
When an error is thrown, it creates an Error object. This object has useful properties, most importantly name and message.
javascript
const err = new TypeError('Not a function');console.log(err.message);
Output
Not a function
Your output
The most powerful part of an error is the stack trace (e.stack). It shows the exact history of which function called which function, leading up to the crash. This tells you where the error happened.
javascript · visualize
function crash() { throw new Error('Boom');}try { crash();} catch (e) { console.log(e.stack.includes('crash'));}
true
Creating Custom Errors
You can create and throw your own errors to stop execution when something is wrong. While you can throw any value (like throw "Oops"), you should always throw an Error object so that you capture the stack trace.
javascript
const err = new Error('Database disconnected');console.log(err instanceof Error);
Output
true
Your output
Parse-Time vs Run-Time
Here is a visual summary of when errors occur:
Where each error type arises: a SyntaxError is thrown in the parse phase before any code runs, while TypeError and ReferenceError happen during execution.
Check yourself
Why can't you catch a SyntaxError with a standard try/catch block in JavaScript?
Reveal answer
SyntaxErrors occur during the parsing phase, before the code even starts executing. — JavaScript parses the entire script first. If it finds invalid syntax, it throws a SyntaxError and never starts running the code, so your try/catch is never executed.
What does a ReferenceError typically mean?
Reveal answer
You tried to use a variable name that hasn't been defined in the current scope. — ReferenceError occurs when you reference a variable that the JavaScript engine cannot find (either it doesn't exist or hasn't been initialized).
If you try to call a string method on a number (e.g., `(42).toUpperCase()`), what kind of error will you get?
Reveal answer
TypeError — TypeError happens when an operation is performed on a value of the wrong type, such as calling a string method on a number.
Challenges
Challenge 1 +25 XP
Fix the code so that it catches the error and correctly identifies whether it is a TypeError or a ReferenceError.
javascript
Test 1 — expects "TypeError\n"
Need a hint? (−25% XP)
Check the `name` property of the error object `e`.