An object stores properties under keys. To append a property to an object in JavaScript, assign a value with dot or bracket notation. If you need to add a value to a JavaScript object without changing the original, create a new object instead and account for any nested references.
Add to JavaScript Object With Dot Notation
To add to a JavaScript object using a known property name that is a valid identifier, assign through a dot:
const user = { name: "Mina" };
user.role = "editor";
console.log(user.role); // "editor"
console.log(user); // { name: "Mina", role: "editor" }
This adds a property to the existing object. The const declaration prevents reassignment of the user binding; it does not freeze the object. Another variable referring to the same object sees the change too. In JavaScript, append to an object only when that mutation is intended.
If the object already has a writable property named role, the assignment replaces its value. It does not create a second property with the same key. For ordinary objects, use Object.hasOwn(user, "role") to distinguish an own property that exists from one that does not. Checking user.role === undefined alone is insufficient because an existing property can have the value undefined.
The distinction matters when applying defaults. If a caller explicitly supplied a key, replacing it may discard a deliberate value. Check ownership before writing when the rule is “add only if missing.” Assignment behavior can also differ for frozen objects, non-writable properties, or setters; these examples use ordinary mutable objects.
Use brackets for dynamic names
Bracket notation accepts an expression for the key. It also handles names that cannot be written as dot identifiers:
const key = "last login";
user[key] = "2027-01-15";
user["account-status"] = "active";
console.log(user["last login"]); // "2027-01-15"
console.log(user["account-status"]); // "active"
Here user[key] uses the value held in key. By contrast, user.key accesses a property literally named key. This distinction matters when adding properties to a JavaScript object based on configuration or another variable. Validate externally supplied property names for the application’s intended use.
You can also use a computed property name when constructing a new object:
const field = "priority";
const task = { title: "Review", [field]: "high" };
console.log(task.priority); // "high"
The expression inside brackets is evaluated to obtain the property key. This is still a property assignment, not an array-style append operation.
For example, const key = "role"; user[key] = "admin"; updates the existing role property in the first example. Replacing "role" with "region" creates a new property instead. Inspect the resulting object or test the specific property after assignment to confirm that the chosen key is the one you intended.
Create a shallow copy
Object spread can add a property while creating a new outer object:
const original = { name: "Mina", settings: { theme: "light" } };
const updated = { ...original, role: "editor" };
console.log(original.role); // undefined
console.log(updated.role); // "editor"
The original outer object has not gained role. If the same key occurs in the spread and later in the literal, the later value wins. Reversing that order changes the result, so put the intended override after the spread.
The copy is shallow. Both objects still refer to the same nested settings object:
updated.settings.theme = "dark";
console.log(original.settings.theme); // "dark"
To change that nested field independently, copy the nested object too:
const separate = {
...original,
settings: { ...original.settings, theme: "blue" }
};
console.log(original.settings.theme); // "dark"
console.log(separate.settings.theme); // "blue"
When adding to objects in JavaScript, copy each modified level if the old state must remain independent. Spread copies own enumerable properties; it does not preserve every property descriptor or the original prototype. For more complex values, choose a cloning strategy suited to the data rather than assuming spread is a deep clone. The MDN property accessor guide and spread reference explain the syntax. Accessed November 20, 2026.
