Skip to main content

How to Add a Property to an Object in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<object>.<name> = <value>;
<object>['<name>'] = <value>;
<object>[<variable>] = <value>;
PartWhat 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:

main.js, which creates a book with only a title, adds year with a dot, author through a variable named field, and first edition with brackets, logs the book, then sets year to 1966 and logs it.

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​

Terminal
node main.js
Output
{
title: 'Dune',
year: 1965,
author: 'Frank Herbert',
'first edition': 'Chilton Books'
}
1966

A zsh terminal where node main.js prints the book with title Dune, year 1965, author Frank Herbert and first edition Chilton Books, then 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 of field, '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.
  • book is a const, and adding properties still worked. const stops the variable from pointing at another object. It doesn't stop the object from changing.
  • Assigning to year again replaced 1965 with 1966. The same assignment adds a property the object doesn't have and replaces one it has.

Common Mistakes​

MistakeWhat happensDo this instead
Using a dot with a variable: book.field = 'Frank Herbert'It adds a property literally called fieldUse 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 objectTypeError: Cannot set properties of undefined (setting 'year')Create the object first, for example const book = {}

Troubleshooting​

SymptomLikely causeFix
The object has a property called field, key or name that you didn't expectA dot was used where the name is in a variableUse object[variable]
An old value was overwrittenThe name you added already existedCheck 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​