JavaScript Interview Question #29

How do you add, update, and delete object properties?

Objects, Arrays & Core Data Handling Junior Beginner

Quick Interview Answer

JavaScript objects are dynamic by default. A property can be added or updated by assigning to it with dot or bracket notation. The delete operator removes an own property when the property is configurable.

Detailed Explanation

JavaScript objects are dynamic by default. A property can be added or updated by assigning to it with dot or bracket notation. The delete operator removes an own property when the property is configurable.

In production code, consider whether mutation is appropriate. Many application patterns instead create a new object with spread syntax to make state changes easier to reason about.

Code Example

const user = { name: "Asif" };

user.age = 30;          // Add
user.name = "M. Asif"; // Update
delete user.age;        // Delete

console.log(user);