# TypeError: Cannot read properties of undefined (reading 'length')

**Code:** `reading length` · **Category:** JavaScript · **Seen on:** JavaScript

Code checked .length on something that is undefined — typically an array or string that hasn’t loaded.

## Meaning

A specific, very common case of the undefined-property error: data from an API, props or state is undefined during the first render or when a field is missing.

## Common causes

- Array/string not initialised
- API field missing
- Accessing .length before async data arrives

## Quick fix

1. Default to empty: (items ?? []).length
2. Initialise state with [] or ''
3. Guard with optional chaining: items?.length

## Detailed fix by platform

### React

1. `const [items, setItems] = useState([]);`

## How to diagnose

1. **Value** — Which variable is undefined?
2. **Timing** — Before data loaded?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/javascript/typeerror-cannot-read-properties-of-undefined-reading-length)
