ShareYourGroupShareYourGroupError Explainer

JavaScript TypeError: Cannot read properties of undefined

A plain-words explanation of this JavaScript message: what it means, the usual causes, and the steps that fix it.

The messageUncaught TypeError: Cannot read properties of undefined (reading 'name')

Make the error yourself

Run in Google Chrome 157.0.8103.51. This is the smallest example we could find that shows it.

JavaScript (in a script tag)

const user = { id: 7 };
console.log(user.profile.name);

Browser console (the real message)

TypeError: Cannot read properties of undefined (reading 'name')

Why it happens

JavaScript tried to read a property (here name) from something that is undefined. In the example user.profile does not exist, so it is undefined, and asking undefined for its name fails. The message shows the property it was reading, in brackets; the thing that is undefined is the object just before it in the chain. Common causes: data from a server that has not arrived yet, a field that is missing in some records, a wrong property name, an array element that does not exist (items[5].name), or code that runs before the page is ready.

Chrome, Edge and Node use these words. Firefox and Safari describe the same problem differently, for example by saying the value is not an object.

How to fix it

  1. Find the object just before the failing property and log it: console.log(user.profile).
  2. Use optional chaining: user.profile?.name returns undefined instead of failing.
  3. Give a fallback with ??: user.profile?.name ?? "unknown".
  4. If data comes from a request, wait for it (await fetch(...)) before you use it.
  5. Check the spelling and capital letters of property names.

The corrected version

JavaScript (in a script tag)

const user = { id: 7 };
console.log(user.profile?.name ?? "no name yet");

Browser console (the corrected version)

no name yet

How to avoid it

Check data at the edge, when it enters your program, and give missing fields default values.

Questions

What is the difference between undefined and null here?

The message says "of undefined" or "of null" depending on what you had. Optional chaining handles both.

This is an independent guide. It is not connected with Python, Microsoft, Google, GitHub or the Git project. Product names belong to their owners. Examples were run on 13 November 2026 in a practice folder or a blank workbook; your version may word a message slightly differently.