01 / 21

JavaScript Errors
& Stack Traces

Learn to read, interpret, and fix common JavaScript errors like ReferenceError and TypeError by decoding their stack traces.

JavaScript

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:

Type
The category of error: ReferenceError, TypeError, SyntaxError, etc.
Message
A human-readable description of what went wrong.
Stack Trace
A trail showing which functions were called before the error occurred.

Anatomy of an Error Message

ReferenceError: username is not defined
Error Type : Error Message Tells you the category of bug Tells you specifically what went wrong

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.

1
Errors pinpoint the problem. The message and line number tell you exactly where to look.
2
Errors prevent silent failures. Without them, broken code would produce wrong results without any warning.
3
Errors teach you the language. Each error reveals a rule about how JavaScript works under the hood.

The Big Three Error Types

ReferenceError Using a variable that doesn't exist Example: console.log(x) when x was never declared TypeError Using a value in an impossible way Example: null.toString() calling a method on null SyntaxError Code that breaks grammar rules Example: if (true { missing closing parenthesis

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.

1 function greet() { 2 let name = "Alice"; 3 } 4 5 greet(); 6 console.log(name); // ReferenceError!
ReferenceError: name is not defined at script.js:6:13

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.

1 let score = 42; 2 score.toUpperCase(); // TypeError!
TypeError: score.toUpperCase is not a function at script.js:2:7

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.

getValue() : line 3 Error happened here TOP processData() : line 8 main() : line 12 <anonymous> : line 15 Entry

Reading a Real Stack Trace

→ TypeError: Cannot read properties of undefined (reading 'name') at displayUser (app.js:14:22) at loadProfile (app.js:9:5) at handleClick (app.js:4:3) at HTMLButtonElement.onclick (index.html:1:1)
1
First line: the error. A TypeError: you tried to read .name on something that is undefined.
2
Second line: the crime scene. It happened in displayUser at app.js, line 14.
3
Remaining lines: the trail. Follow the chain: handleClick called loadProfile, which called displayUser.

How to Debug Step by Step

1
Read the error type. Is it a ReferenceError, TypeError, or SyntaxError? This tells you the category of problem.
2
Read the message. What specific name, property, or value is mentioned? This is your primary clue.
3
Find the file and line number. Open the file mentioned in the first "at" line and go to that exact line.
4
Inspect the variable. Add console.log() before the error line to see what the variable actually holds.
5
Trace back through the stack. If the value came from another function, follow the stack trace to find where it went wrong.

Example 1: Typo Bug

1 let totalPrice = 29.99; 2 let tax = totalprice * 0.08; // Bug here! 3 console.log(tax);
ReferenceError: totalprice is not defined at script.js:2:11

Example 2: Null Access

1 function getUser(id) { 2 if (id === 1) return { name: "Alice" }; 3 return null; 4 } 5 6 let user = getUser(5); 7 console.log(user.name); // TypeError!
TypeError: Cannot read properties of null (reading 'name') at script.js:7:18

Example 3: Following the Stack

1 function formatName(user) { 2 return user.name.toUpperCase(); 3 } 4 function printBadge(data) { 5 let label = formatName(data); 6 console.log("Badge: " + label); 7 } 8 printBadge({ age: 25 }); // No "name" property!
TypeError: Cannot read properties of undefined (reading 'toUpperCase') at formatName (script.js:2:24) at printBadge (script.js:5:17)

Common Mistakes

✕ Only reading the first word
Students see "Error" and panic. Always read the full message including the variable name and line number.
✕ Ignoring the stack trace
The stack trace is a map to the bug. Skipping it means guessing instead of investigating.
✕ Fixing the wrong line
The error line shows where it crashed, not always where the bug originated. Trace back through the stack.

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

What does this error tell you?

Question 3 of 5

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

The console shows ReferenceError: doubel is not defined. What is the best fix?