← Back to all tutorials

Updating Properties

Change, add, and delete properties on objects — understand how to modify objects after they are created.

Updating Properties

Objects in JavaScript are mutable — you can change their properties, add new ones, and delete existing ones after creation.

Updating Existing Properties

var user = {
    name: 'Shaun',
    age: 30,
    email: 'shaun@example.com'
};

user.name = 'Luigi';
user.age = 25;

console.log(user.name);  // 'Luigi'
console.log(user.age);   // 25

Adding New Properties

user.location = 'Manchester';
user.isAdmin = true;

console.log(user.location);  // 'Manchester'
console.log(user.isAdmin);   // true

Deleting Properties

delete user.age;

console.log(user.age);  // undefined

Updating Methods

// Replace an existing method
user.greet = function() {
    console.log('Hey! I am ' + this.name);
};

// Add a new method
user.logInfo = function() {
    console.log(this.name + ' - ' + this.email);
};

user.greet();    // 'Hey! I am Luigi'
user.logInfo();  // 'Luigi - shaun@example.com'

Important: Value vs Reference

var userOne = { name: 'Shaun', age: 30 };
var userTwo = userOne;  // Both point to the SAME object

userTwo.name = 'Mario';

console.log(userOne.name);  // 'Mario' (also changed!)
console.log(userTwo.name);  // 'Mario'

When you assign an object to another variable, both variables point to the same object in memory. Changing one changes the other. This is called pass by reference.

Key Takeaways

  • Use dot notation or bracket notation to update existing properties
  • Assigning to a new property name adds it to the object
  • delete obj.prop removes a property
  • Objects are passed by reference — assigning to a new variable does not create a copy