0
0
Javascriptprogramming~5 mins

Adding and removing properties in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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: <br>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: <br>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);
How do you remove the property 'name' from the object 'user'?
Auser.remove('name');
Buser.name = null;
Cdelete user.name;
Duser['name'] = undefined;
What will be the output of console.log(obj.nonExistentProp); if 'nonExistentProp' does not exist on 'obj'?
Anull
BError
C0
Dundefined
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
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
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.