JavaScript Error

Cannot Read Properties of Undefined

"Cannot read properties of undefined (reading 'x')" is the most common TypeError in JavaScript. It occurs when you try to access a property or call a method on a value that is undefined or null.

Common causes

  • Accessing a property on an API response before it loads
  • Deeply nested object access where an intermediate property is missing
  • Array element access at an out-of-bounds index
  • Function returning undefined instead of an object
  • Destructuring from an undefined source

How to fix it

  • Use optional chaining (?.) for safe property access: obj?.prop?.nested
  • Provide default values with nullish coalescing (??): value ?? "default"
  • Check for existence before accessing: if (obj && obj.prop)
  • Initialize variables with default values
  • Use default parameters in function arguments

Example

Cannot Read Properties of Undefined example
// Error example
const response = undefined;
console.log(response.data);
// TypeError: Cannot read properties of undefined (reading 'data')

const users = [{ name: "Alice" }];
console.log(users[5].name);
// TypeError: Cannot read properties of undefined (reading 'name')

// Fix with optional chaining
console.log(response?.data);  // undefined (no error)
console.log(users[5]?.name);  // undefined (no error)

// Fix with nullish coalescing
const data = response?.data ?? [];

// Fix with default parameter
function greet(user = { name: "Guest" }) {
  return `Hello, ${user.name}`;
}

Track Cannot Read Properties of Undefined with Checkend

Checkend automatically captures Cannot Read Properties of Undefined errors in your JavaScript application with full context:

  • Complete stack trace with source maps
  • Browser and environment context
  • Automatic grouping of similar errors
  • Instant notifications when errors occur

Stop debugging in production

Get full error context and fix issues faster with self-hosted error tracking.