JavaScript Errors

JavaScript TypeError: Cannot read properties of undefined โ€” Fixed

JavaScript TypeError: Cannot read properties of undefined

What Does This Error Mean?

JavaScript throws TypeError: Cannot read properties of undefined (reading 'x') when you try to access a property or call a method on a value that is undefined. The runtime expected an object but got nothing. It is one of the most common runtime errors in every JS codebase.

Common Causes (With Code)

1. Accessing a property on a function that returns nothing

Functions that don't explicitly return a value always return undefined. Chaining a property access on the result crashes immediately.

โŒ Causes the error

function getUser() {
  const user = { name: "Alice" };
  // forgot to return!
}

const user = getUser();
console.log(user.name);
// TypeError: Cannot read properties of undefined (reading 'name')

2. Async data that hasn't loaded yet

Accessing a property before the async response arrives means the variable is still undefined.

โŒ Causes the error

let config;

fetch("/api/config")
  .then(res => res.json())
  .then(data => { config = data; });

// Runs BEFORE fetch resolves โ€” config is still undefined
console.log(config.timeout);
// TypeError: Cannot read properties of undefined (reading 'timeout')

3. A missing key in a nested object

Drilling into a deeply nested object with a key that doesn't exist produces undefined at that level, and the next dot access crashes.

โŒ Causes the error

const response = { data: { users: [] } };

// 'profile' key doesn't exist on data
console.log(response.data.profile.name);
// TypeError: Cannot read properties of undefined (reading 'name')

How to Fix It

Fix 1 โ€” Add the missing return statement

โœ… Correct

function getUser() {
  const user = { name: "Alice" };
  return user; // โ† explicit return
}

const user = getUser();
console.log(user.name); // "Alice"

Fix 2 โ€” Use Optional Chaining (?.)

Optional Chaining short-circuits to undefined instead of throwing when any link in the chain is nullish. Combine with ?? to provide a safe default.

โœ… Correct

const response = { data: {} };

// Safe โ€” returns undefined instead of crashing
const name = response?.data?.profile?.name ?? "Anonymous";
console.log(name); // "Anonymous"

Fix 3 โ€” Guard with an explicit null-check before access

โœ… Correct

async function loadConfig() {
  const res = await fetch("/api/config");
  const config = await res.json();

  if (config) {
    console.log(config.timeout); // safe โ€” config is defined here
  }
}

Fix 4 โ€” Provide default values with destructuring

โœ… Correct

// Destructure with a default to avoid undefined objects
const { profile = {} } = response.data;
console.log(profile.name ?? "No name"); // never crashes

Frequently Asked Questions

What's the difference between null and undefined in this context?

Both cause a similar error (Cannot read properties of null vs โ€ฆof undefined), but null is an explicit empty value you assigned, while undefined means the variable was never given a value. Optional Chaining (?.) guards against both.

Does Optional Chaining work in all browsers?

Yes โ€” ?. is supported in all modern browsers (Chrome 80+, Firefox 74+, Safari 13.1+, Edge 80+) and Node.js 14+. For older environments, transpile with Babel. Do not use it in IE11 without a polyfill.

How do I know which property triggered the error?

The error message tells you exactly: (reading 'name') is the property that was being accessed on the undefined value. Check the stack trace line number to find where in your code the chain broke.