V8-based (Chrome, Edge)
- Message
- TypeError: Cannot read properties of undefined (reading 'x')
JavaScript threw because code read a property of undefined or null. Find which value is empty, then fix it with a check, optional chaining or a default value.
Uncaught TypeError: Cannot read properties of undefined means your code tried to read a property from a value that is undefined. Find the value just before the dot in the message, work out why it's empty at that moment, then guard it with a check or optional chaining (?.), or fix the code that should have set it.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
In Cannot read properties of undefined (reading 'id'), the word after reading is the property you asked for. The empty value is whatever you wrote before .id. A message that says null instead of undefined means that value is null; the fix is the same.
const foo = undefined;
foo.substring(1); // TypeError: foo is undefined
const foo2 = null;
foo2.substring(1); // TypeError: foo2 is nullMDN confirms the querySelector row: if no matches are found, null is returned.
The shorter modern form is optional chaining. MDN says that if the object accessed or function called with ?. is undefined or null, the expression short-circuits and evaluates to undefined instead of throwing. It also warns that the older obj.first && obj.first.second pattern isn't safe for falsy values such as 0, and that ?. avoids that.
const city = customer?.city ?? "Unknown city";
const first = list?.[0];
const label = button?.getLabel?.();if (foo !== undefined && foo !== null) {
// Now we know that foo is defined, we are good to go.
}
if (foo) {
// Now we know that foo is truthy, it will necessarily not be null/undefined.
}Open DevTools, then Console. The error links to the exact line.
Read the message. Everything left of the final dot in the failing expression is the candidate.
Use console.log on the value and on its parent. You're looking for the first link in the chain that is undefined or null.
If the value comes from a request, a script that loads later or a user action, the read may be running too early. Move the read, or render only once the data exists.
Use a check if you need a different branch, or ?. with ?? if a default is fine.
A close relative is ReferenceError: x is not defined, which is thrown when the name itself doesn't exist, not when its value is empty.
4 questions people ask next, answered from the sources on this page.
Find the value before the dot, then find out why it is empty at that moment. Fix the source, or guard the read with a check or optional chaining.
Both cause the same TypeError when you read a property from them. MDN lists null and undefined together as the usual unexpected values behind this error.
The name in quotes is the property your code tried to read. The empty value is the object you tried to read it from, so look at what comes before .id.
It prevents the throw by returning undefined. It's right when the value may legitimately be missing. If the value should exist, it only hides the real bug.
Run the free audit, then approve the fixes. Every change can be undone.