Back to course

Adding, Modifying, and Deleting Properties

JavaScript: The Complete '0 to Hero' Beginner Course

64. Adding, Modifying, and Deleting Properties

Objects are mutable; we can change their structure easily.

1. Adding New Properties

Assign a value to a new key using either dot or bracket notation.

javascript const book = { title: 'JS Basics' };

book.author = 'A. Coder'; // Add using dot notation book['pages'] = 350; // Add using bracket notation

console.log(book); // { title: 'JS Basics', author: 'A. Coder', pages: 350 }

2. Modifying Existing Properties

Simply re-assign a new value to an existing key.

javascript book.pages = 400; // Update the page count

3. Deleting Properties

Use the delete operator to completely remove a property from the object.

javascript delete book.pages;

console.log(book); // { title: 'JS Basics', author: 'A. Coder' }

Checking for Property Existence

Use the in operator or check if the property value is undefined.

javascript console.log('author' in book); // true