Open In App

Modifying Objects in JavaScript

Last Updated : 29 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Modifying objects in JavaScript allows you to change, add, or delete properties dynamically, offering flexibility for managing and manipulating data within applications.

Adding Properties to an Object

One of the most common tasks when working with objects in JavaScript is adding new properties. JavaScript provides two primary ways to add properties to an object:

1. Using Dot Notation to Add Properties

Dot notation is the simplest and most commonly used method for adding properties to an object. You reference the object name followed by a dot and then the property name.

Now let's understand this with the help of example:

JavaScript
let car = {
  brand: "Tesla",
  model: "Model 3",
  year: 2022
};

car.color = "red";  // Adding a new property 'color'
console.log(car);

Output

{ brand: 'Tesla', model: 'Model 3', year: 2022, color: 'red' }

In this example

  • We added a new property called color to the car object.
  • The value of color is 'red'. The addition is simple and intuitive.

2. Using Bracket Notation to Add Properties

Bracket notation is used when the property name is dynamic or if it contains characters that are not allowed in dot notation (such as spaces or symbols). Bracket notation allows for greater flexibility because the property name can be a string or stored in a variable.

Now let's understand this with the help of example:

JavaScript
let car = {
  brand: "Tesla",
  model: "Model 3",
  year: 2022
};

car["engineType"] = "Electric";  // Using bracket notation
console.log(car);

Output

{ brand: 'Tesla', model: 'Model 3', year: 2022, color: 'red', engineType: 'Electric' }

In this example

  • we used bracket notation to add a new property 'engineType' with value 'Electric'.

Updating Properties of an Object

JavaScript allows you to modify the value of an existing property. You can update properties using both dot and bracket notation.

1. Using Dot Notation to Update Properties

Dot notation provides a clean and straightforward way to update properties.

Now let's understand this with the help of an example:

JavaScript
let car = {
  brand: "Tesla",
  model: "Model 3",
  year: 2022
};

car.model = "Model S";
car.year = 2023;
console.log(car);

Output

{ brand: 'Tesla', model: 'Model S', year: 2023 }

In this example:

  • We updated the model property from "Model 3" to "Model S".
  • We updated the year property from 2022 to 2023.

2. Using Bracket Notation to Update Properties

Bracket notation allows updating properties using strings or dynamic variables.

Now let's understand this with the help of an example:

JavaScript
let car = {
  brand: "Tesla",
  model: "Model 3",
  year: 2023,
  color: "red",
  engineType: "Electric"
};

car["model"] = "Model S";  // Changing the model property
console.log(car);

Output

{
brand: 'Tesla',
model: 'Model S',
year: 2023,
color: 'red',
engineType: 'Electric'
}

In this example:

  • We used bracket notation to update the model property.
  • This is especially useful when the property name is stored in a variable or not a valid identifier.

Deleting Properties from an Object

When managing objects, there may be situations where a property is no longer needed. JavaScript offers the delete operator to remove such properties from an object permanently.

JavaScript
let car = {
  brand: "Tesla",
  model: "Model S",
  year: 2023,
  color: "red"
};

delete car.color;  // Deleting the 'color' property
console.log(car);

Output

{ brand: 'Tesla', model: 'Model S', year: 2023, engineType: 'Electric' }

In this example

  • we used the delete operator to remove the color property from the car object.
  • Once a property is deleted, it no longer exists within the object.

Working with Nested Objects

In JavaScript, objects can contain other objects as values for their properties, creating a structure known as nested objects. Nested objects allow you to represent more complex data hierarchies.

Now let's understand this with the help of example

JavaScript
let employee = {
  name: "John",
  position: "Developer",
  contact: {
    email: "[email protected]",
    phone: "555-1234"
  }
};
console.log(employee);

Output

{
name: 'John',
position: 'Developer',
contact: { email: '[email protected]', phone: '555-1234' }
}

In this example

  • The example defines an employee object.
  • It has three properties: name, position, and contact.
  • name is set to "John".
  • position is set to "Developer".
  • contact holds email and phone information.

Adding or Updating Nested Properties

To add or update properties within a nested object, you can access the nested object first, then use dot or bracket notation.

JavaScript
let employee = {
  name: "John",
  position: "Developer",
  contact: {
    email: "[email protected]",
    phone: "555-1234"
  }
};
employee.contact.address = "123 Main St";  // Adding a new property to the nested object
console.log(employee);

Output

{
name: 'John',
position: 'Developer',
contact: {
email: '[email protected]',
phone: '555-1234',
address: '123 Main St'
}
}

In this example

  • employee.contact.address = "123 Main St"; adds a new address property to the contact object.
  • The address property is set to "123 Main St".
  • console.log(employee); prints the updated employee object, showing the new address property.

Deleting Nested Properties

Properties from a nested object can be deleted using the delete operator by accessing the property with dot or bracket notation and then applying delete.

JavaScript
let employee = {
  name: "John",
  position: "Developer",
  contact: {
    email: "[email protected]",
    phone: "555-1234",
    address: "123 Main St"
  }
};
delete employee.contact.address;  // Deleting the address property
console.log(employee);

Output

{
name: 'John',
position: 'Developer',
contact: { email: '[email protected]', phone: '555-1234' }
}

In this example

  • delete employee.contact.address; removes the address property from the contact object.
  • The address property is no longer part of the employee object.
  • console.log(employee); prints the updated employee object without the address property.

Using Arrays as Object Properties

JavaScript objects can store arrays as values for their properties. This is useful when you need to represent multiple related values under a single property.

Now let's understand this with the help of example

JavaScript
let book = {
  title: "The Great Gatsby",
  author: "F. Scott Fitzgerald",
  genres: ["Fiction", "Classic", "Literary"]
};
console.log(book)

Output

{
title: "The Great Gatsby",
author: "F. Scott Fitzgerald",
genres: ["Fiction", "Classic", "Literary"]
}

In this example

  • book is an object with three properties: title, author, and genres.
  • title is "The Great Gatsby", author is "F. Scott Fitzgerald", and genres is an array with three elements: ["Fiction", "Classic", "Literary"].
  • console.log(book); prints the entire book object to the console.

Modifying Array Properties in an Object

Modifying Array Properties in an Object allows changes to array elements within an object using methods like push(), pop(), or direct index modification.

JavaScript
let book = { title: "1984", genres: ["Fiction", "Dystopian"] };
book.genres.push("Classic");       // Adding
book.genres[1] = "Political";     // Updating
book.genres.pop();                // Removing
console.log(book);

Output

{ title: "1984", genres: ["Fiction", "Political"] }

In this example

  • book.genres.push("Classic");: This line adds a new genre, "Drama", to the genres array in the book object.
  • After this operation, the genres array is updated to: ["Fiction", "Classic", "Literary", "Drama"].
  • console.log(book);: This logs the entire book object to the console, showing the updated genres array that now includes "Drama".

Updating Array Elements Inside an Object

Updating Array Elements Inside an Object involves directly modifying an array property within an object by accessing its elements through their index.

JavaScript
let book = {
  title: "The Great Gatsby",
  author: "F. Scott Fitzgerald",
  genres: ["Fiction", "Classic", "Literary"]
};
book.genres[1] = "Historical Fiction";  // Changing the second genre
console.log(book);

Output

{
title: "The Great Gatsby",
author: "F. Scott Fitzgerald",
genres: ["Fiction", "Historical Fiction", "Literary"]
}

In this example

  • The book object initially has the genres array as ["Fiction", "Classic", "Literary"].
  • book.genres[1] = "Historical Fiction"; modifies the second element (index 1) of the genres array, changing it from "Classic" to "Historical Fiction".
  • console.log(book); prints the updated book object, reflecting this change in the genres array. The second genre is now "Historical Fiction".

Article Tags :

Similar Reads