# Uncaught TypeError: Cannot read properties of undefined

> 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.

Canonical: https://scaplet.com/errors/browser/uncaught-typeerror-cannot-read-properties-of-undefined

Guide Error · Browser

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.

4 min

read

4

sources

Oct 24, 2026

updated

Scaplet Editorial Team

author

In one line

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.

30-second check

## Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?

Summarize this page with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fbrowser%2Funcaught-typeerror-cannot-read-properties-of-undefined%20(%22Uncaught%20TypeError%3A%20Cannot%20read%20properties%20of%20undefined%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fbrowser%2Funcaught-typeerror-cannot-read-properties-of-undefined%20(%22Uncaught%20TypeError%3A%20Cannot%20read%20properties%20of%20undefined%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fbrowser%2Funcaught-typeerror-cannot-read-properties-of-undefined%20(%22Uncaught%20TypeError%3A%20Cannot%20read%20properties%20of%20undefined%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fbrowser%2Funcaught-typeerror-cannot-read-properties-of-undefined%20(%22Uncaught%20TypeError%3A%20Cannot%20read%20properties%20of%20undefined%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

On this page

## 5 parts, 4 minutes.

1. [What the message means and who causes it 01](#what-the-message-means-and-who-causes-it)
2. [Why the value is empty 02](#why-the-value-is-empty)
3. [Fix it 03](#fix-it)
4. [Track it down in two minutes 04](#track-it-down-in-two-minutes)
5. [What it does to SEO 05](#what-it-does-to-seo)

_01_The same error in each engine, per MDN

## What the message means and who causes it

MDN files this error under "x is (not) y" and says it occurs "when there was an unexpected type. Oftentimes, unexpected undefined or null values." The error comes from your own code, or a script you load, and the browser is only reporting it. The text in brackets names the property you were reading, and the [thing](/glossary/entity) it was read from is the value that is empty.

Browser engine

### V8-based (Chrome, Edge)

Message

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

Browser engine

### Firefox

Message

TypeError: "x" is undefined, or TypeError: "undefined" is not an object

Browser engine

### Safari

Message

TypeError: undefined is not an object (evaluating 'obj.x')

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.

_02_Common ways a value ends up empty

## Why the value is empty

MDN's own example is the plain case: you cannot invoke a method on an undefined or null variable.

Cause

### A lookup found nothing

Example

document.querySelector(".menu") returns null when nothing matches, so .classList fails

How to confirm

Log the lookup result before using it

Cause

### A property chain breaks in the middle

Example

user.address.city when user.address is missing

How to confirm

Log each step of the chain

Cause

### Data hasn't arrived yet

Example

Code reads response data before the request has finished

How to confirm

Check the order: is the read inside the callback or after await?

Cause

### A misnamed property

Example

data.itmes instead of data.items

How to confirm

Log the object and compare the keys

Cause

### A function returns nothing

Example

A helper with no return statement

How to confirm

Log the return value

MDN's example

```
const foo = undefined; foo.substring(1); // TypeError: foo is undefined const foo2 = null; foo2.substring(1); // TypeError: foo2 is null
```

MDN confirms the querySelector row: if no matches are found, `null` is returned.

_03_Check it

## Fix it

MDN gives two guards. Test the value first, or, if you're confident it won't be another falsy value such as "" or 0, test truthiness.

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.

Optional chaining with a default

```
const city = customer?.city ?? "Unknown city"; const first = list?.[0]; const label = button?.getLabel?.();
```

**Don't use ?. to hide a bug**Optional chaining stops the error, it doesn't supply the missing data. If the value should always exist, find out why it doesn't. Also note MDN: `?.` can't be used on the left side of an assignment, so `object?.property = 1` is a SyntaxError.

Check before you read

```
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. }
```

_04_How it works

## Track it down in two minutes

1. 1

### Click the file and line

Open DevTools, then Console. The error links to the exact line.
2. 2

### Find the empty value

Read the message. Everything left of the final dot in the failing expression is the candidate.
3. 3

### Log it just before the line

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.
4. 4

### Ask when it was set

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.
5. 5

### Pick the guard

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](/errors/browser/uncaught-referenceerror-is-not-defined), which is thrown when the name itself doesn't exist, not when its value is empty.

_05_Explained

## What it does to SEO

This error is raised by the browser's JavaScript engine, not returned by your server. If a script that produces text or links throws before it finishes, the page may render without them. See [JavaScript SEO](/glossary/javascript-seo) and [rendering](/glossary/rendering).

Sources · 4

## Every fact, linked and dated.

Official docs first. The date is when we last opened each page.

- mozilla.org

1. 01 [MDN: TypeError: "x" is (not) "y"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_type) developer.mozilla.org · checked 2026-10-25
2. 02 [MDN: Optional chaining (?.)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) developer.mozilla.org · checked 2026-10-25
3. 03 [MDN: Document.querySelector()](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) developer.mozilla.org · checked 2026-10-25
4. 04 [MDN: undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined) developer.mozilla.org · checked 2026-10-25

Questions

## Asked a lot.

4 questions people ask next, answered from the sources on this page.

_01_ How do I fix "cannot read properties of undefined"?

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.

_02_ What is the difference between null and undefined here?

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.

_03_ Why does it say reading 'id'?

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.

_04_ Is optional chaining a fix?

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.

## Keep learning

[All errors](/errors)

[Learn Uncaught ReferenceError: x is not defined Browser errors](/errors/browser/uncaught-referenceerror-is-not-defined) [Related Client-side rendering Crawling and indexing](/glossary/client-side-rendering) [Read next Text content does not match server-rendered HTML Browser errors](/errors/browser/text-content-does-not-match-server-rendered-html) [Do Run a free site audit Free, about two minutes](/site-audit)

More Uncaught errors
[Uncaught ReferenceError: x is not defined](/errors/browser/uncaught-referenceerror-is-not-defined)

More in Browser errors
[Access-Control-Allow-Origin](/errors/browser/access-control-allow-origin) [Aw, Snap!](/errors/browser/aw-snap) [blocked:mixed-content](/errors/browser/blocked-mixed-content) [Chrome out of memory](/errors/browser/chrome-out-of-memory) [Firefox Server Not Found](/errors/browser/firefox-server-not-found) [Firefox: "Secure Connection Failed"](/errors/browser/firefox-secure-connection-failed) [Firefox: File not found](/errors/browser/firefox-file-not-found) [Firefox: the connection has timed out](/errors/browser/firefox-the-connection-has-timed-out) [Firefox: Warning: Potential Security Risk Ahead](/errors/browser/firefox-warning-potential-security-risk-ahead) [Refused to apply style ... because its MIME type](/errors/browser/refused-to-apply-style-from-because-its-mime-type) [Safari can't find the server](/errors/browser/safari-can-t-find-the-server) [Safari can't open the page](/errors/browser/safari-can-t-open-the-page) [Safari: "This Connection Is Not Secure" and related warnings](/errors/browser/safari-this-connection-is-not-private) [SSL certificate problem: unable to get local issuer certificate](/errors/browser/ssl-certificate-problem-unable-to-get-local-issuer-certificate) [Text content does not match server-rendered HTML](/errors/browser/text-content-does-not-match-server-rendered-html) [This page isn't working](/errors/browser/this-page-isn-t-working) [This site can't provide a secure connection](/errors/browser/this-site-can-t-provide-a-secure-connection) [Uncaught ReferenceError: x is not defined](/errors/browser/uncaught-referenceerror-is-not-defined) [Uncaught TypeError: Cannot read properties of undefined](/errors/browser/uncaught-typeerror-cannot-read-properties-of-undefined) [Your connection is not private](/errors/browser/your-connection-is-not-private) [Your connection is not private in Chrome (on every site)](/errors/browser/your-connection-is-not-private-chrome)

Terms on this page
[Entity](/glossary/entity) [JavaScript SEO](/glossary/javascript-seo) [Rendering](/glossary/rendering)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

Run the free audit, then approve the fixes. Every change can be undone.

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
