"Cannot read properties of undefined (reading 'X')" in JavaScript — What It Means and How to Fix It
What the JavaScript error "Cannot read properties of undefined (reading 'X')" means and how to fix it.
Published October 29, 2026
This TypeError means you tried to access a property on undefined (or null). The value you expected to be an object does not exist yet.
What it means and why it happens
Typical causes: async data not loaded, wrong nested path, missing function return values or array index out of range.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
TypeError: Cannot read properties of undefined (reading 'name')
Practical examples
Code that triggers it
const user = undefined
console.log(user.name)The fix
console.log(user?.name ?? 'Guest')
const { name = 'Guest' } = user ?? {}React-style guard
if (!data) return null
return <h1>{data.title}</h1>Common mistakes
- Rendering before data arrives.
- Chaining without optional chaining.
- Wrong response shape (data.data.items).
Key takeaways
- TypeError: Cannot read properties of undefined: This TypeError means you tried to access a property on undefined (or null).
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
What is the word in parentheses?
The property name you tried to read.
How do I find which line caused TypeError: Cannot read properties of undefined?
The error message and stack trace show the file and line; start there and work backwards.