DEV Community

Emily Scott
Emily Scott

Posted on

JavaScript 'Cannot Read Properties of Undefined' Error (How to Fix It Step by Step Guide)

JavaScript Cannot Read Properties of Undefined (How to Fix It Step by Step)

This is one of the most common and frustrating JavaScript errors developers face:

TypeError: Cannot read properties of undefined
Enter fullscreen mode Exit fullscreen mode

It appears suddenly.

Your code was working.

Then one missing value breaks everything.

This happens in:

  • API responses
  • React components
  • Form handling
  • DOM manipulation
  • Database results
  • Nested objects
  • User authentication flows

Almost every JavaScript developer has faced this error.

Let’s fix it properly.


The Problem

Suppose you write this:

const user = {
  name: "John"
};

console.log(user.address.city);
Enter fullscreen mode Exit fullscreen mode

Expected Output

New York
Enter fullscreen mode Exit fullscreen mode

Actual Output

TypeError: Cannot read properties of undefined (reading 'city')
Enter fullscreen mode Exit fullscreen mode

Very annoying.

Why?

Because address does not exist.

JavaScript tries to read this:

undefined.city
Enter fullscreen mode Exit fullscreen mode

And that causes the crash.


Why This Happens

JavaScript reads objects step by step.

This line:

user.address.city
Enter fullscreen mode Exit fullscreen mode

means:

  1. Find user
  2. Find address
  3. Find city

But if step 2 returns undefined, step 3 fails immediately.

JavaScript cannot read properties from undefined.

That is the root problem.


Real-World Example

This happens constantly with APIs.

Example:

const response = await fetchUser();

console.log(response.data.profile.name);
Enter fullscreen mode Exit fullscreen mode

Looks normal.

But if this is missing:

response.data
Enter fullscreen mode Exit fullscreen mode

your app crashes.

This is why frontend bugs happen so often after API updates.

Very common in production.


First Fix: Check Before Accessing

Safer version:

if (user.address) {
  console.log(user.address.city);
}
Enter fullscreen mode Exit fullscreen mode

Now JavaScript checks first.

No crash.

Simple and safe.

But this becomes messy with deeply nested objects.

Example:

if (
  response &&
  response.data &&
  response.data.profile
) {
  console.log(response.data.profile.name);
}
Enter fullscreen mode Exit fullscreen mode

Too much code.

Hard to maintain.


Better Fix: Optional Chaining

Modern JavaScript gives us a much better solution:

console.log(user.address?.city);
Enter fullscreen mode Exit fullscreen mode

This means:

Only continue if address exists.

If it does not exist, JavaScript returns undefined instead of crashing.

Much cleaner.

Much better.


Real API Example with Optional Chaining

Instead of this:

if (
  response &&
  response.data &&
  response.data.profile
) {
  console.log(response.data.profile.name);
}
Enter fullscreen mode Exit fullscreen mode

Write this:

console.log(response.data?.profile?.name);
Enter fullscreen mode Exit fullscreen mode

Short.

Readable.

Production-friendly.

This is the preferred modern solution.


Another Useful Fix: Default Values

Sometimes you want a fallback value.

Example:

console.log(user.address?.city || "City not available");
Enter fullscreen mode Exit fullscreen mode

Output

City not available
Enter fullscreen mode Exit fullscreen mode

Very useful for UI rendering.

Especially in React apps.

Users should see fallback text—not app crashes.


React Version of This Problem

This error happens constantly in React:

<h1>{user.profile.name}</h1>
Enter fullscreen mode Exit fullscreen mode

If user.profile is missing, the component crashes.

Safer version:

<h1>{user.profile?.name}</h1>
Enter fullscreen mode Exit fullscreen mode

Much safer.

Cleaner code.

Less production stress.

React developers use this daily.


Hidden Cause: Async Data Loading

Another common reason:

console.log(users[0].name);
Enter fullscreen mode Exit fullscreen mode

If the API has not loaded yet:

users[0]
Enter fullscreen mode Exit fullscreen mode

is undefined.

Crash again.

Safer version:

console.log(users[0]?.name);
Enter fullscreen mode Exit fullscreen mode

Always protect async-loaded data.

Very important.


Quick Debug Rule

Whenever you see:

Cannot read properties of undefined
Enter fullscreen mode Exit fullscreen mode

Ask yourself:

Which value is actually undefined?

Not the final property.

The missing step before it.

That is where the real bug lives.

This saves hours.


Final Thoughts

This error is not random.

It happens because JavaScript reaches undefined before reaching the property you want.

Remember:

  • Check before accessing
  • Use optional chaining ?.
  • Add fallback values when needed
  • Protect API responses
  • Protect React rendering
  • Protect async-loaded data

Mastering this one error removes a huge amount of frontend bugs.

Probably more than any other JavaScript fix.


Your Turn

What was your worst “Cannot read properties of undefined” bug?

Most developers remember that painful day very clearly.

Peace,
Emily Idioms

Top comments (0)