JavaScript Errors
& Stack Traces
Learn to read, interpret, and fix common JavaScript errors like ReferenceError and TypeError by decoding their stack traces.
What Is a JavaScript Error?
When JavaScript encounters code it cannot execute, it creates an Error object and stops running the current function. This is called "throwing" an error. Every error object contains three critical pieces of information:
Anatomy of an Error Message
Why Errors Matter
Errors are not punishments: they are messages from the JavaScript engine trying to help you. Professional developers encounter errors constantly and treat them as the fastest way to diagnose bugs.
The Big Three Error Types
ReferenceError Deep Dive
A ReferenceError occurs when you try to use a variable or function name that JavaScript cannot find in any accessible scope. The engine searched every scope in the chain and came up empty.
TypeError Deep Dive
A TypeError means the variable exists, but you tried to use it in a way its type doesn't support. Think of it as calling a fish to fly: the fish is real, but it can't do that.
ReferenceError vs TypeError
| ReferenceError | TypeError | |
|---|---|---|
| Variable exists? | No | Yes |
| Core problem | Name not found in scope | Wrong operation for the type |
| Common cause | Typo, wrong scope, not declared | null/undefined access, wrong method |
| Message pattern | X is not defined | Cannot read property / is not a function |
| Fix strategy | Check spelling and scope | Check value's type before using it |
What Is a Stack Trace?
A stack trace is a list of function calls that were active when the error happened. It reads top to bottom: the topmost entry is where the error actually occurred, and each line below shows which function called the one above it.
Reading a Real Stack Trace
How to Debug Step by Step
Example 1: Typo Bug
Example 2: Null Access
Example 3: Following the Stack
Common Mistakes
Error Type Cheat Sheet
| Error Type | When It Fires | Common Message | Fix |
|---|---|---|---|
| ReferenceError | Variable not found | X is not defined | Check spelling & scope |
| TypeError | Wrong operation on type | Cannot read / is not a function | Check type & null |
| SyntaxError | Invalid code grammar | Unexpected token | Check brackets & quotes |
| RangeError | Value out of bounds | Maximum call stack exceeded | Check recursion & inputs |
Question 1 of 5
You see this error in the console:
ReferenceError: userName is not defined
What is the most likely cause?
Question 2 of 5
Code:
let items = null;
console.log(items.length);
Console output:
TypeError: Cannot read properties of null (reading 'length')
What does this error tell you?
Question 3 of 5
Stack trace:
TypeError: x.push is not a function
at addItem (utils.js:12:5)
at processOrder (main.js:27:3)
at checkout (main.js:45:7)
In which function and file should you start debugging?
Question 4 of 5
A student writes:
consol.log("hello");
What error type will JavaScript throw?
Question 5 of 5
Code:
function double(n) {
return n * 2;
}
let result = doubel(5);
console.log(result);
The console shows ReferenceError: doubel is not defined. What is the best fix?