TypeError: Cannot read properties of undefined (reading 'length')
Code checked .length on something that is undefined — typically an array or string that hasn’t loaded.
Seen on:
JavaScript
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
- Default to empty: (items ?? []).length
- Initialise state with [] or ''
- Guard with optional chaining: items?.length
Detailed fix by platform
React
const [items, setItems] = useState([]);
How to diagnose
- Value — Which variable is undefined?
- Timing — Before data loaded?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026