We add or remove properties to change what information an object holds. This helps us update or clean up data easily.
Adding and removing properties in Javascript
To add a property:
object.propertyName = value;
To remove a property:
delete object.propertyName;You can add properties using dot notation or bracket notation.
Use delete keyword to remove a property from an object.
name with value 'Alice' to the person object.const person = {}; person.name = 'Alice';
brand property from the car object.const car = { brand: 'Toyota' }; delete car.brand;
title using bracket notation.const book = {}; book['title'] = 'JavaScript Guide';
This program starts with a user object having username and age. It adds an email property and removes the age property. Finally, it prints the updated object.
const user = { username: 'coder123', age: 25 }; // Add a new property user.email = 'coder123@example.com'; // Remove the age property delete user.age; console.log(user);
Adding a property that already exists will update its value.
Removing a property that does not exist does nothing and does not cause errors.
Use bracket notation when property names have spaces or special characters.
You can add properties to objects using object.property = value.
Remove properties using delete object.property.
This helps keep objects up-to-date and clean.
Practice
Which of the following is the correct way to add a new property age with value 25 to an existing object person?
Solution
Step 1: Understand how to add properties in JavaScript objects
Properties are added by assigning a value toobject.property.Step 2: Check each option's syntax
Onlyperson.age = 25;uses correct JavaScript syntax to add a property.Final Answer:
person.age = 25; -> Option AQuick Check:
Adding property = person.age = 25 [OK]
- Using invalid syntax like 'add' keyword
- Using arrow or parentheses incorrectly
- Trying to call property as a function
Which of the following lines correctly removes the property color from the object car?
Solution
Step 1: Recall how to remove properties in JavaScript
Thedeletekeyword removes a property from an object.Step 2: Verify each option
Onlydelete car.color;correctly removes the property.Final Answer:
delete car.color; -> Option DQuick Check:
Remove property = delete object.property [OK]
- Using 'remove' keyword which doesn't exist
- Assigning null instead of deleting
- Calling delete as a method
What will be the output of the following code?
const obj = {a: 1};
obj.b = 2;
delete obj.a;
console.log(obj);Solution
Step 1: Add property 'b' to object
Afterobj.b = 2;, object is{a:1, b:2}.Step 2: Delete property 'a'
delete obj.a;removes property 'a', so object becomes{b:2}.Final Answer:
{b: 2} -> Option BQuick Check:
Delete removes property completely [OK]
- Thinking delete sets property to undefined
- Expecting original property to remain
- Confusing object output format
Find the error in the following code that tries to remove the name property from user:
const user = {name: 'Alice', age: 30};
remove user.name;Solution
Step 1: Check the keyword used for deletion
JavaScript usesdelete, notremove, to delete properties.Step 2: Confirm object and property validity
useris defined andnameis a valid property.Final Answer:
The keywordremoveis invalid in JavaScript. -> Option AQuick Check:
Use 'delete' keyword, not 'remove' [OK]
- Using 'remove' instead of 'delete'
- Trying to set property to null to delete
- Assuming property can't be deleted
You have an object settings with properties {theme: 'dark', fontSize: 14, showSidebar: true}. You want to remove showSidebar only if it exists, then add a new property language with value 'en'. Which code correctly does this?
Solution
Step 1: Check if property exists before deleting
Using'showSidebar' in settingsensures property exists before deletion.Step 2: Add new property correctly
settings.language = 'en';adds the new property properly.Step 3: Verify other options
delete settings.showSidebar; settings.language == 'en';
uses wrong comparison operator==instead of assignment=.settings.showSidebar = undefined; settings.language = 'en';
sets property to undefined but does not remove it.if (settings.showSidebar) { settings.showSidebar = null; } settings.language = 'en';sets property to null but does not remove it.Final Answer:
if ('showSidebar' in settings) { delete settings.showSidebar; } settings.language = 'en'; -> Option CQuick Check:
Check existence, delete, then add property [OK]
- Using '==' instead of '=' for assignment
- Setting property to undefined or null instead of deleting
- Not checking if property exists before deleting
Ready to prove it?
Pass the test and this pattern moves unknown
