This TypeError is thrown when you try to assign a property to a value that is undefined. Unlike reading a property from undefined, writing to it is never silently ignored: it always throws immediately.
Why it happens
- •A variable is declared but never assigned an object before property assignment.
- •A function returns undefined when it was expected to return an object.
- •An async operation has not completed yet and the result is still undefined.
- •A typo in a variable name references the wrong (undefined) binding.
Examples
Code that triggers the error
let user;
user.name = "Alice";Error output
TypeError: Cannot set properties of undefined (setting 'name')
How to fix it
Initialize variables to an empty object ({}) or the appropriate default before assigning properties. Use optional chaining (?.) for reading, but remember it does not help for writing. Add null checks or default values when a function might return undefined.
Fixed code
let user = {};
user.name = "Alice";
console.log(user.name); // "Alice"Related errors
Ready to practice your English at work?
Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.
Try Lingua-e for free