SKU // 114

JavaScript Debugging Errors

Error messages look scary, but most of them mean something simple, and they are clues: they tell you what kind of mistake happened and where to look. This lesson is a field guide to the messages you will see most, what each one means, and how to fix it.

// How to Read an Error Message

An error in the console has three important parts:

Uncaught TypeError: Cannot read properties of undefined (reading 'name') script.js:12 at showUser (script.js:12:22) at start (script.js:20:3)
Part Meaning
TypeErrorThe error type: the kind of mistake
Cannot read properties of undefined (reading 'name')The explanation, often naming the exact value or property
script.js:12The file and line number: click it to jump there
at showUser ... at startThe stack: showUser was running, called by start
Rule
Fix the first error before anything else. One mistake often causes several more errors further down, and they disappear once the first one is fixed.

// Cannot read properties of undefined (reading '...')

The most common error in JavaScript. The name in quotes is the property it was trying to read, so the value before the dot was undefined (or null):

let user;
console.log(user.name);
TypeError: Cannot read properties of undefined (reading 'name')

Find out why user is empty: log it, and check where it should have been set. If it is allowed to be missing, use optional chaining (JS Objects) and a fallback:

let user;
console.log(user?.name ?? "Guest");
Guest
Note
W3Schools shortens this message to "Cannot read properties of undefined." Chrome adds the most useful part, the property name: (reading 'name').

// ... is not defined

console.log(myValue);
ReferenceError: myValue is not defined

The name does not exist where you used it. Check the spelling (including capital letters), that it was declared with let or const, and that it is in scope: a variable declared inside a function or block does not exist outside it (JS Scope).

// ... is not a function

console.log("hello".toUppercase());
TypeError: "hello".toUppercase is not a function

You called something that is not a function. Usually a misspelled method name (here, it should be toUpperCase with a capital C), a method that does not exist on that type (like calling .map on an object), or a variable that holds a value instead of a function.

// Cannot access ... before initialization

console.log(total);
let total = 5;
ReferenceError: Cannot access 'total' before initialization

A let or const variable was used above the line that declares it (the temporal dead zone, JS Hoisting). Move the declaration up.

// Assignment to constant variable

const score = 10;
score = 11;
TypeError: Assignment to constant variable.

You tried to reassign a const. Use let if the value really needs to change.

// SyntaxError Messages

A SyntaxError means JavaScript cannot understand the code at all, so the script does not run. The line number points to where JavaScript noticed the problem, which can be just after the actual mistake:

Code Message Usual cause
if (x == 5 {Unexpected token '{'A missing )
console.log("hi"missing ) after argument listA call that is never closed
function f() { return 1;Unexpected end of inputA missing } somewhere above
let name = "Ava;Invalid or unexpected tokenA string that is never closed
let score = 1; let score = 2;Identifier 'score' has already been declaredThe same let/const name declared twice

// More Messages You Will Meet

Message Means Fix
x is not iterablefor...of or spread used on something that is not a listCheck that x is an array, string, Map, or Set
Maximum call stack size exceededA function calls itself foreverAdd a stopping condition
Invalid array lengthAn array created with a negative or huge sizeCheck the number passed to new Array()
Unexpected token '<' ... is not valid JSONParsing HTML (often an error page) as JSONCheck the URL and response.ok (Debug Async)

// NaN Is Not an Error

NaN never shows up as an error message. It silently appears in your results when math fails:

let result = "abc" * 5;
console.log(result);
NaN

When you see NaN, log each input to the calculation and find the one that is not a real number (JS NaN).

// Summary

ReferenceErrorA name is not defined (yet)
TypeErrorA value is used incorrectly: often undefined or null
SyntaxErrorThe code structure is broken
RangeErrorA number is out of range
NaN (no error)Invalid math somewhere upstream

// Quick Check

Which value was undefined?

const order = { customer: { name: "Ava" } };
console.log(order.shipping.city);
// TypeError: Cannot read properties of undefined (reading 'city')
Show answer
order.shipping. The message names the property being read (city), so the value just before it was the undefined one: order has no shipping property.

What does TypeError: list.forEach is not a function most likely mean?

  1. list is not declared
  2. list is not an array
  3. The page has a syntax error
  4. forEach is misspelled in the error
Show answer
B. list exists (otherwise it would be a ReferenceError), but it holds something without a forEach method, like an object or a string.

The console shows eight errors. Which one should you fix first, and why?

Show answer
The first one. Later errors are often caused by the first (for example, a value that failed to load is then undefined everywhere it is used), so they may disappear once it is fixed.

// Try It

  1. Click Open Playground and run it. Then cause "is not a function" and "reading a property of undefined" on purpose and read each message
  2. Write a line with a missing ) and one with a missing }, and compare the messages
  3. Fix a "Cannot read properties of undefined" error with ?. and ??
  4. Find the first error in a page with several errors, fix it, and see which others disappear
// Video Walkthroughs
Last updated: Oct 10, 2026, 1:50 AM ET