Bird
Raised Fist0
Javascriptprogramming~20 mins

Adding and removing properties in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Property Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after adding a property?
Consider the following JavaScript code. What will be logged to the console?
Javascript
const obj = {a: 1};
obj.b = 2;
console.log(obj);
ATypeError
B{"a":1}
Cundefined
D{"a":1,"b":2}
Attempts:
2 left
πŸ’‘ Hint
Adding a property to an object uses dot notation and changes the object.
❓ Predict Output
intermediate
2:00remaining
What happens after deleting a property?
What will be the output of this code snippet?
Javascript
const obj = {x: 10, y: 20};
delete obj.x;
console.log(obj);
A{"x":10,"y":20}
B{"y":20}
Cundefined
DSyntaxError
Attempts:
2 left
πŸ’‘ Hint
The delete operator removes a property from an object.
❓ Predict Output
advanced
2:00remaining
What is the output after adding a property with a variable key?
What will this code print to the console?
Javascript
const key = 'color';
const obj = {shape: 'circle'};
obj[key] = 'red';
console.log(obj);
A{"shape":"circle","key":"red"}
B{"color":"red"}
C{"shape":"circle","color":"red"}
DReferenceError
Attempts:
2 left
πŸ’‘ Hint
Bracket notation allows using variables as property keys.
❓ Predict Output
advanced
2:00remaining
What error occurs when deleting a non-configurable property?
What will happen when running this code?
Javascript
const obj = {};
Object.defineProperty(obj, 'fixed', {
  value: 42,
  configurable: false
});
delete obj.fixed;
console.log(obj.fixed);
A42
Bundefined
CTypeError
DSyntaxError
Attempts:
2 left
πŸ’‘ Hint
Non-configurable properties cannot be deleted.
🧠 Conceptual
expert
2:00remaining
Which option causes a runtime error when adding/removing properties?
Which of the following code snippets will cause a runtime error when executed?
Aconst obj = Object.freeze({a: 1}); obj.b = 2;
Bconst obj = {a: 1}; delete obj.a;
Cconst obj = {}; obj['b'] = 3;
Dconst obj = {a: 1}; obj.a = 5;
Attempts:
2 left
πŸ’‘ Hint
Frozen objects cannot be changed; adding properties throws error in strict mode.

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

  1. Step 1: Understand how to add properties in JavaScript objects

    Properties are added by assigning a value to object.property.
  2. Step 2: Check each option's syntax

    Only person.age = 25; uses correct JavaScript syntax to add a property.
  3. Final Answer:

    person.age = 25; -> Option A
  4. 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

  1. Step 1: Recall how to remove properties in JavaScript

    The delete keyword removes a property from an object.
  2. Step 2: Verify each option

    Only delete car.color; correctly removes the property.
  3. Final Answer:

    delete car.color; -> Option D
  4. Quick Check:

    Remove property = delete object.property [OK]
Hint: Use 'delete object.property;' to remove properties [OK]
Common Mistakes:
  • Using 'remove' keyword which doesn't exist
  • Assigning null instead of deleting
  • Calling delete as a method
3.

What will be the output of the following code?

const obj = {a: 1};
obj.b = 2;
delete obj.a;
console.log(obj);
medium
A. {a: 1, b: 2}
B. {b: 2}
C. {a: undefined, b: 2}
D. {}

Solution

  1. Step 1: Add property 'b' to object

    After obj.b = 2;, object is {a:1, b:2}.
  2. Step 2: Delete property 'a'

    delete obj.a; removes property 'a', so object becomes {b:2}.
  3. Final Answer:

    {b: 2} -> Option B
  4. 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

  1. Step 1: Check the keyword used for deletion

    JavaScript uses delete, not remove, to delete properties.
  2. Step 2: Confirm object and property validity

    user is defined and name is a valid property.
  3. Final Answer:

    The keyword remove is invalid in JavaScript. -> Option A
  4. 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?

hard
A.
settings.showSidebar = undefined;
settings.language = 'en';
B.
delete settings.showSidebar;
settings.language == 'en';
C.
if ('showSidebar' in settings) {
  delete settings.showSidebar;
}
settings.language = 'en';
D.
if (settings.showSidebar) {
  settings.showSidebar = null;
}
settings.language = 'en';

Solution

  1. Step 1: Check if property exists before deleting

    Using 'showSidebar' in settings ensures property exists before deletion.
  2. Step 2: Add new property correctly

    settings.language = 'en'; adds the new property properly.
  3. 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.
  4. Final Answer:

    if ('showSidebar' in settings) { delete settings.showSidebar; } settings.language = 'en'; -> Option C
  5. Quick Check:

    Check existence, delete, then add property [OK]
Hint: Check property with 'in' before delete; assign new property with '=' [OK]
Common Mistakes:
  • Using '==' instead of '=' for assignment
  • Setting property to undefined or null instead of deleting
  • Not checking if property exists before deleting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard