Modifying Objects in JavaScript
Last Updated :
29 Jul, 2025
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".
Similar Reads
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.Basics of Web Development To better understand t
7 min read
HTML Tutorial
CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read
JS Tutorial
JavaScript TutorialJavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.Client Side: On the client side, JavaScript works
11 min read
JSON TutorialJSON (JavaScript Object Notation) is a widely-used, lightweight data format for representing structured data. Used Extensively : Used in APIs, configuration files, and data exchange between servers and clients.Text-based: JSON is a simple text format, making it lightweight and easy to transmit.Human
5 min read
TypeScript TutorialTypeScript is a superset of JavaScript that adds extra features like static typing, interfaces, enums, and more. Essentially, TypeScript is JavaScript with additional syntax for defining types, making it a powerful tool for building scalable and maintainable applications.Static typing allows you to
8 min read
Vue.js TutorialVue.js is a progressive JavaScript framework for building user interfaces. It stands out for its simplicity, seamless integration with other libraries, and reactive data binding.Built on JavaScript for flexible and component-based development.Supports declarative rendering, reactivity, and two-way d
4 min read
jQuery TutorialjQuery is a lightweight JavaScript library that simplifies the HTML DOM manipulating, event handling, and creating dynamic web experiences. The main purpose of jQuery is to simplify the usage of JavaScript on websites. jQuery achieves this by providing concise, single-line methods for complex JavaSc
8 min read
Front End
React TutorialReact is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
Angular TutorialAngular is a powerful, open-source web application framework for building dynamic and scalable single-page applications (SPAs). Developed by Google, Angular provides a comprehensive solution for front-end development with tools for routing, form handling, HTTP services, and more.Designed for buildin
4 min read
Backend
Node.js TutorialNode.js is a powerful, open-source, and cross-platform JavaScript runtime environment built on Chrome's V8 engine. It allows you to run JavaScript code outside the browser, making it ideal for building scalable server-side and networking applications.JavaScript was mainly used for frontend developme
4 min read
Express.js TutorialExpress.js is a minimal and flexible Node.js web application framework that provides a list of features for building web and mobile applications easily. It simplifies the development of server-side applications by offering an easy-to-use API for routing, middleware, and HTTP utilities.Built on Node.
4 min read
PHP TutorialPHP is a popular, open-source scripting language mainly used in web development. It runs on the server side and generates dynamic content that is displayed on a web application. PHP is easy to embed in HTML, and it allows developers to create interactive web pages and handle tasks like database mana
9 min read
Laravel TutorialLaravel is an open-source PHP web application framework that has gained immense popularity since its inception in 2011, created by Taylor Otwell. This renowned framework empowers developers to build robust, scalable web applications with remarkable ease. As a developer-friendly framework, Laravel of
3 min read
Database
Web Technologies Questions The following Web Technologies Questions section contains a wide collection of web-based questions. These questions are categorized based on the topics HTML, CSS, JavaScript, and many more. Each section contains a bulk of questions with multiple solutions. Table of Content HTML QuestionsCSS Question
15+ min read