How to Add a Property to an Object in JavaScript
To add a property to a JavaScript object, assign a value to a name the object doesn't have yet: book.year = 1965 or book['year'] = 1965. Square brackets also take a variable that holds the name, or a name a dot can't take, such as one with a space in it.
Syntax
<object>.<name> = <value>;
<object>['<name>'] = <value>;
<object>[<variable>] = <value>;
| Part | What it does |
|---|---|
<object> | The object that gets the property. |
.<name> | Dot notation: the property's name, written as it is. It must be a valid JavaScript name, so no spaces. |
['<name>'] | Bracket notation: the name as a string, which may hold any characters. |
[<variable>] | Bracket notation with a variable: the property's name is the value the variable holds. |
<value> | Any value: a string, a number, an array, another object. |
If the object already has a property with that name, the same assignment replaces its value. MDN's Working with objects guide: "You can always add a property to a previously defined object."
The Example
The example starts with a book that has only a title. It adds a year with a dot, an author through a variable that holds the name, and a first edition property whose name has a space in it. At the end, it assigns to year again.
Prerequisites
- Node.js. This guide uses Node.js 26.13.1.
Step 1: Write the Code
Create a file named main.js:

const book = { title: 'Dune' };
book.year = 1965;
const field = 'author';
book[field] = 'Frank Herbert';
book['first edition'] = 'Chilton Books';
console.log(book);
book.year = 1966;
console.log(book.year);
Step 2: Run It
node main.js
{
title: 'Dune',
year: 1965,
author: 'Frank Herbert',
'first edition': 'Chilton Books'
}
1966

The command is the same in zsh, bash and PowerShell.
What Happened
- The book went from one property to four, in the order the code added them.
book[field]used the value offield,'author', as the name. The variable's own name doesn't matter.'first edition'needed brackets, because a name with a space can't follow a dot. Node.js prints it in quotes for the same reason.bookis aconst, and adding properties still worked.conststops the variable from pointing at another object. It doesn't stop the object from changing.- Assigning to
yearagain replaced1965with1966. The same assignment adds a property the object doesn't have and replaces one it has.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using a dot with a variable: book.field = 'Frank Herbert' | It adds a property literally called field | Use brackets: book[field] |
Using a dot with a name that has a space: book.first edition = … | SyntaxError: Unexpected identifier 'edition' | Use brackets with a string: book['first edition'] |
| Adding a property to a variable that holds no object | TypeError: Cannot set properties of undefined (setting 'year') | Create the object first, for example const book = {} |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The object has a property called field, key or name that you didn't expect | A dot was used where the name is in a variable | Use object[variable] |
| An old value was overwritten | The name you added already existed | Check first with 'year' in book, or pick another name |
FAQ
How do I add several properties at once?
Object.assign(book, { year: 1965, author: 'Frank Herbert' }) adds both to book. To get a new object and leave the old one as it is, use spread syntax: const copy = { ...book, year: 1965 }.
How do I remove a property?
Use delete: delete book.year removes the property, and the object no longer has it.
Is there a difference between book.year and book['year']?
No. Both name the same property. Brackets are needed only when the name is in a variable, or when it isn't a valid JavaScript name.
Conclusion
Assigning a value to a name adds the property, or replaces its value if the object already has it. Use a dot when you know the name and it's a plain word, and square brackets when the name is in a variable or has characters a dot can't take.
Additional Resources
- Working with objects, on MDN.
- Property accessors, on MDN.
