Error Reference › JavaScript
JavaScript
Cannot read properties of undefined / null
You tried to read a property off a value that was undefined or null — the thing you expected to be an object wasn't there.
What it looks like
Uncaught TypeError: Cannot read properties of undefined (reading 'name')
at renderUser (app.js:42:18)
What does "Cannot read properties of undefined / null" mean?
Thrown by JavaScript at the moment you access a property on undefined or null. The part in brackets names the property you were trying to read, and the first stack line gives the exact file and line.
What causes "Cannot read properties of undefined / null"?
- A variable/API response was undefined when you accessed it.
- An object key doesn't exist.
- Async data used before it loaded.
How do I confirm the cause?
- Go to the line in the stack trace and log the object on the left of the failing property (for user.name, log user).
- If the value comes from fetch or an API, log the whole response — the shape often differs from what the code expects (data.user vs user).
- If it fails only on first render, the data has not loaded yet when the code runs.
How do I fix "Cannot read properties of undefined / null"?
- Log the value right before the failing line.
- Use optional chaining (obj?.prop) and defaults.
- Guard against null/undefined before accessing.
- For async data, render only after it has loaded.
How do I stop it happening again?
- Use optional chaining (user?.name) and defaults (?? '') where a value can legitimately be missing.
- Validate API responses at the boundary, or type them with TypeScript, so bad shapes fail loudly in one place.