JavaScript Error

Fix: Uncaught TypeError: Cannot read properties of undefined

By Samrat•Last updated: Nov 2026

Exact message

Uncaught TypeError: Cannot read properties of undefined (reading 'map')

Quick answer

This error occurs when you try to access a property (like .map, .length, or .name) on a variable that is currently undefined. The most common fix is optional chaining (?.) or ensuring the variable is initialized.

🛠️ Fix it right here with AI Code Explainer

Edit the traceback below if needed, then let our AI suggest a fix without leaving this page.

AI suggestions can be wrong. Review and test before applying.

Why this error happens

JavaScript evaluates accessors from left to right. If the object is undefined, accessing .map or .length throws a TypeError. In React, this usually happens when component state is initialized as undefined instead of an empty array [].

Example: Wrong vs Right

Wrong

const items = undefined;
items.map(item => item);
// Uncaught TypeError: Cannot read properties of undefined (reading 'map')

Right

const items = undefined;

// Option 1: Optional Chaining
items?.map(item => item);

// Option 2: Fallback to empty array
(items || []).map(item => item);

How to fix it (step by step)

  1. Find the line in the console error and identify which variable is undefined
  2. Use console.log() right before that line to confirm the variable's value
  3. Apply optional chaining (?.) if the property might legitimately be undefined
  4. If it's React state, ensure your initial useState value is an empty array [] and not null or undefined

Frequently asked questions

What does optional chaining (?.) do?

It safely accesses nested properties. If the reference before the ?. is null or undefined, the expression short-circuits and returns undefined instead of throwing.

Why does this happen with API data?

When you fetch data asynchronously, the component renders before the data arrives. The initial state is often undefined. Use a loading state or default value.

Is 'Cannot read property of undefined' the same?

Yes, it's the older V8 engine phrasing. The fix is exactly the same.

Related Search Terms

cannot read property map of undefinedreading 'map' of undefineduncaught typeerror cannot read propertiescannot read properties of undefined react

Related JS errors

Need to fix JS code not just errors?

Use the free to paste any JS snippet and get a line-by-line explanation.

Want an AI with memory for your project?

MonsterX AI remembers your codebase across chats.