Ad Space — Top Banner

Cannot read properties of undefined

JavaScript Programming Language

Severity: Moderate

What Does This Error Mean?

This error means you tried to access a property or method on a variable that is undefined. Undefined means the variable exists but has never been given a value. JavaScript cannot look up properties on an empty variable, so it stops and throws this error.

Affected Models

  • All browsers
  • Node.js
  • Deno
  • Any JavaScript runtime

Common Causes

  • Accessing a property of an object that was declared but never assigned a value
  • A function returned undefined because it has no return statement
  • Accessing an array index that does not exist, like myArray[10] when the array only has 3 items
  • Destructuring an object and one of the expected properties does not exist
  • Trying to use the result of an async function without awaiting it first

How to Fix It

  1. Find the line in the error message. Use console.log() on the variable right before that line to see what it actually contains.

    If the console prints 'undefined', you have confirmed the problem. Now trace back to where it should have been assigned.

  2. Check if the variable was assigned a value. Look for the place in your code where it should be set, and make sure that code actually ran.

    A variable declared with let myVar; but never assigned a value will always be undefined.

  3. If you are accessing an array item, check the array length first: if (index < myArray.length) { ... }

    Accessing myArray[5] when the array only has 3 items returns undefined, and then .property crashes.

  4. If the data comes from an API or external source, check whether the expected properties actually exist in the response.

    APIs can return different shapes of data depending on the situation. Never assume a property will always be present.

  5. Use optional chaining (?.) to prevent the crash: let value = myObject?.property?.subProperty;

    If myObject is undefined, this safely returns undefined instead of throwing an error.

When to Call a Professional

This is one of the most common JavaScript errors and is always fixable without outside help. The solution is to check that a variable has a value before accessing its properties. Every JavaScript developer encounters this error regularly.

Frequently Asked Questions

What does undefined actually mean in JavaScript?

Undefined means a variable has been declared (created) but never assigned a value. For example: let myVar; — myVar now exists but its value is undefined. It also appears when a function has no return statement and you try to use its result. JavaScript uses undefined as the automatic default for 'no value assigned'.

Why does my function return undefined?

A JavaScript function returns undefined if it has no return statement, or if it has return; with no value. If you expect a function to return something (like an object or number), make sure it has a return statement that actually returns the data. Also check that the return statement is not inside an if block that never runs.

How is this different from the null properties error?

They are almost identical errors — the only difference is the type of empty value. 'Cannot read properties of null' means the variable is null (intentionally empty). 'Cannot read properties of undefined' means the variable was never assigned at all. The fix is the same for both: check the value exists before using it.