TypeError: Cannot read properties of undefined (reading 'name')
TypeError: Cannot read property 'name' of null
TypeError: Cannot read property 'map' of undefined
- Async data not loaded yet — rendering before data arrives
- Missing null check — assuming a value exists when it might not
- Wrong API response shape — expecting
data.userbut gettingdata - Typo in property name — accessing
user.naeminstead ofuser.name
Add a null/undefined check before accessing the property:
// Before
console.log(user.name);
// After
console.log(user?.name); // Optional chaining
console.log(user && user.name); // Short-circuit (ES5)// ❌ Crashes if user is null/undefined
const name = user.name;
const city = user.address.city;
const count = items.length;
// Safe with optional chaining
const name = user?.name;
const city = user?.address?.city;
const count = items?.length ?? 0;
// With function calls
const result = obj?.method?.();
// With arrays
const first = arr?.[0]?.name;const name = user?.name ?? 'Anonymous';
const items = response?.data?.items ?? [];
const count = data?.total ?? 0;// React: show loading until data arrives
function UserProfile({ userId }) {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
fetchUser(userId)
.then(setUser)
.finally(() => setLoading(false));
}, [userId]);
if (loading) return <p>Loading...</p>;
if (!user) return <p>User not found</p>;
// Now safe to access user.name
return <h1>{user.name}</h1>;
}- Use TypeScript to catch null-related errors at compile time
- Always initialize state with sensible defaults
- Validate API responses before using them