reading length 🟨 JavaScript

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

  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?

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