TypeError: Cannot Read Properties of Undefined — Explained & Fixed
What Does This Error Mean?
This TypeError occurs when you try to access a property or method on a value that is undefined. JavaScript is telling you: "The thing before the dot doesn't exist."
Common Causes
- Accessing a property on an object that hasn't been initialized
- API response data that hasn't loaded yet
- Typo in a variable or property name
- Array index out of bounds returning undefined
How to Fix It
1. Optional Chaining (?.)
// Before (crashes)
const name = user.profile.name
// After (safe)
const name = user?.profile?.name2. Default Values
const items = response.data || []
items.map(item => ...)3. Guard Clauses
if (!user || !user.profile) return null