Home » How to Update Key with New Value in JavaScript: 5 Methods

How to Update Key with New Value in JavaScript: 5 Methods

How to Update Key with New Value in JavaScript 5 Different Examples

As a JavaScript developer, you’ll frequently work with objects with key-value pairs. Sometimes, you may need to update the value of an existing key or add a new key-value pair to an object. In this article, we’ll explore different techniques to update object keys with new values in JavaScript. This article is especially for newbies to show examples of “How to Update Key with New Value in JavaScript?”

First, let’s consider the basic structure of an object in JavaScript. An object is a data structure that holds key-value pairs. A key is a unique identifier for a value, which can be of any data type, including strings, numbers, and even other objects.

To update a key with a new value in JavaScript, you can use bracket notation or the foreach method. Here are five different ways to do it:

Bracket Notation

One of the most common ways to update an object key in JavaScript is to use bracket notation. This technique involves accessing the key you want to update using the bracket notation syntax and assigning a new value to it.

For example, let’s say we have an object called ‘person’ with the key-value pair ‘name: John’. We can update the ‘name’ key with a new value using the following code:

In this example, we’ve used the bracket notation syntax to access the ‘name’ key and assigned a new value to it. The object ‘person’ now has the key-value pair ‘name: Jane’ instead of ‘name: John’.

ForEach Method

Another technique to update an object key in JavaScript is to use the foreach method to iterate over the object’s key-value pairs. This method allows you to perform a function on each key-value pair in the object.

We can use the foreach method to update the ‘name’ key with a new value as follows:

In this example, we’ve used the foreach method to iterate over the ‘person’ object’s key-value pairs. When the loop encounters the ‘name’ key, it assigns a new value of ‘Jane’ to it. The object ‘person’ now has the key-value pair ‘name: Jane’ instead of ‘name: John’.

Spread Operator

The spread operator is another way to update an object key in JavaScript. This technique involves creating a new object that includes the original object’s key-value pairs and the new key-value pair you want to add.

We can use the spread operator to add a new key-value pair called ‘age: 30’ to the object as follows:

In this example, we’ve created a new object called ‘newPerson’ that includes the original object’s key-value pair and the new key-value pair ‘age: 30’. The original object ‘person’ remains unchanged.

Object.assign Method

The object.assign method is another technique to update an object key in JavaScript. This method allows you to create a new object that includes the original object’s key-value pairs and the new key-value pair you want to add.

We can use the object.assign method to add a new key-value pair called ‘age: 30’ to the object as follows:

In this example, we’ve created a new object called ‘newPerson’ that includes the original object’s key-value pair and the new key-value pair ‘age: 30’. The original object ‘person’ remains unchanged.

Dot Notation

Dot notation is another way to update an object key in JavaScript. This technique involves accessing the key you want to update using the dot notation syntax and assigning a new value to it.

We can update the ‘name’ key with a new value using the following code:

In this example, we’ve used the dot notation syntax to access the ‘name’ key and assigned a new value to it. The object ‘person’ now has the key-value pair ‘name: Jane’ instead of ‘name: John’.


In conclusion, updating an object key with a new value in JavaScript is a common task every developer needs to know. You can use different techniques to update an object key, such as bracket notation, foreach method, spread operator, object.assign method, and dot notation. Each method has advantages and disadvantages, so choosing the one that best suits your needs are up to you.

Just to remind you, the original object is modified when you update an object key. You can use the spread operator or object to create a new object that includes the updated key-value pairs.assign method. These methods return an array that includes the updated key-value pairs.

Finally, understanding how to update object keys in JavaScript is essential for building complex applications that involve data structures. By mastering this skill, you’ll be able to build more efficient and powerful applications that meet your users’ needs.

Ilyas Ozkurt

Hello I'm İlyas Özkurt. I am a software developer who has been working on websites for 10 years. My first studies in this field were during my high school years. Now I work as a software developer at 6harf and am responsible for all web projects there.

Add comment