How do you add a new property to an existing JavaScript object?
You add a new property by assigning a value to it using dot notation or bracket notation. For example: obj.newProp = value; or obj['newProp'] = value;
Click to reveal answer
beginner
How do you remove a property from a JavaScript object?
You remove a property using the delete operator. For example: delete obj.propName; removes the property propName from obj.
Click to reveal answer
beginner
What happens if you try to access a property that does not exist on an object?
If a property does not exist, accessing it returns undefined. It does not cause an error.
Click to reveal answer
intermediate
Can you add properties to objects created from classes or functions in JavaScript?
Yes, JavaScript objects are flexible. You can add properties to any object, including those created from classes or constructor functions, by assigning new properties.
Click to reveal answer
intermediate
What is the difference between dot notation and bracket notation when adding or removing properties?
Dot notation uses a fixed property name (like obj.prop). Bracket notation uses a string or variable (like obj['prop'] or obj[varName]). Bracket notation is useful when property names have spaces or special characters or are dynamic.
Click to reveal answer
Which of the following adds a property 'age' with value 30 to the object 'person'?
ABoth B and C
Bperson['age'] = 30;
Cperson.age = 30;
Dperson.add('age', 30);
✗ Incorrect
Both dot notation and bracket notation can add properties to an object.
How do you remove the property 'name' from the object 'user'?
Auser.remove('name');
Buser.name = null;
Cdelete user.name;
Duser['name'] = undefined;
✗ Incorrect
The 'delete' operator removes a property from an object.
What will be the output of console.log(obj.nonExistentProp); if 'nonExistentProp' does not exist on 'obj'?
Anull
BError
C0
Dundefined
✗ Incorrect
Accessing a non-existing property returns 'undefined' in JavaScript.
Which notation allows you to use a variable as the property name when adding a property?
ABracket notation
BNeither
CBoth dot and bracket notation
DDot notation
✗ Incorrect
Bracket notation allows dynamic property names using variables.
If you set a property to undefined, does it remove the property from the object?
AYes, it removes the property
BNo, the property still exists with value undefined
CIt depends on the object type
DIt throws an error
✗ Incorrect
Setting a property to undefined does not remove it; the property still exists.
Explain how to add and remove properties from a JavaScript object with examples.
Think about how you assign values to new keys and how you delete keys.
You got /4 concepts.
Describe the difference between setting a property to undefined and deleting a property from an object.
Consider what happens to the property key in each case.
You got /3 concepts.
Practice
(1/5)
1.
Which of the following is the correct way to add a new property age with value 25 to an existing object person?
easy
A. person.age = 25;
B. add person.age = 25;
C. person->age = 25;
D. person(age) = 25;
Solution
Step 1: Understand how to add properties in JavaScript objects
Properties are added by assigning a value to object.property.
Step 2: Check each option's syntax
Only person.age = 25; uses correct JavaScript syntax to add a property.
Final Answer:
person.age = 25; -> Option A
Quick Check:
Adding property = person.age = 25 [OK]
Hint: Use dot notation: object.property = value to add [OK]
Common Mistakes:
Using invalid syntax like 'add' keyword
Using arrow or parentheses incorrectly
Trying to call property as a function
2.
Which of the following lines correctly removes the property color from the object car?
easy
A. car.color = null;
B. remove car.color;
C. car.delete(color);
D. delete car.color;
Solution
Step 1: Recall how to remove properties in JavaScript
The delete keyword removes a property from an object.
Step 2: Verify each option
Only delete car.color; correctly removes the property.
Final Answer:
delete car.color; -> Option D
Quick Check:
Remove property = delete object.property [OK]
Hint: Use 'delete object.property;' to remove properties [OK]
delete obj.a; removes property 'a', so object becomes {b:2}.
Final Answer:
{b: 2} -> Option B
Quick Check:
Delete removes property completely [OK]
Hint: Deleting removes property; it doesn't set undefined [OK]
Common Mistakes:
Thinking delete sets property to undefined
Expecting original property to remain
Confusing object output format
4.
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;
medium
A. The keyword remove is invalid in JavaScript.
B. The property name cannot be deleted.
C. The object user is not defined.
D. The property should be set to null instead.
Solution
Step 1: Check the keyword used for deletion
JavaScript uses delete, not remove, to delete properties.
Step 2: Confirm object and property validity
user is defined and name is a valid property.
Final Answer:
The keyword remove is invalid in JavaScript. -> Option A
Quick Check:
Use 'delete' keyword, not 'remove' [OK]
Hint: Use 'delete' keyword to remove properties, not 'remove' [OK]
Common Mistakes:
Using 'remove' instead of 'delete'
Trying to set property to null to delete
Assuming property can't be deleted
5.
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?