Javascript · Objects
Deleting Properties
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — The delete Keyword
To remove a property completely, use the `delete` keyword followed by the property. Zap's nickname is dropped:
const zap = { name: "Zap", nickname: "Zappy", level: 3 }; delete zap.nickname; console.log(zap.nickname); console.log(zap.name);
After `delete`, the key is gone: reading it gives `undefined`, like any missing property. The other properties are untouched.
Exercise 2 — Gone vs Empty
Setting a property to `undefined` is not the same as deleting it: the key still exists, it just has no value. The `in` operator tells the two apart:
const zap = { nickname: "Zappy", status: "asleep" }; zap.nickname = undefined; delete zap.status; console.log("nickname" in zap); console.log("status" in zap);
`"key" in object` answers `true` if the key exists, whatever its value. Most of the time, `delete` is what you want when something should really disappear.
Exercise 3 — Quiz: After delete
What does this code print?
const bag = { potions: 3, gold: 120 }; delete bag.gold; console.log(bag.gold);
Options
• ```undefined```
• ```120```
• ```0```
• An error
Hint : A deleted key behaves like a key that was never there.
Exercise 4 — Cure the Status
Pyrochu is no longer asleep: remove its temporary `status` property.
const pyrochu = { name: "Pyrochu", status: "asleep" }; ____ pyrochu.____; console.log("status" in pyrochu);
Options: delete status remove "status" undefined
Hint : One keyword removes a property, and the property is written the way you'd read it.
Exercise 5 — delete With Brackets
`delete` accepts the bracket notation too, which means the key can come from a variable:
const zap = { name: "Zap", bonus: 10, "temp effect": "paralyzed" }; let key = "bonus"; delete zap[key]; delete zap["temp effect"]; console.log("bonus" in zap); console.log("temp effect" in zap);
Deleting a key that doesn't exist is not an error either: nothing happens.
Exercise 6 — Quiz: Still There?
What does this code print?
const zap = { level: 3, shiny: true }; delete zap.shiny; console.log("shiny" in zap);
Options
• ```false```
• ```true```
• ```undefined```
• An error
Hint : This operator checks whether the key exists at all.
Exercise 7 — Remove by Variable
Remove the property named in `key`, then check it's gone using that same variable.
const zap = { name: "Zap", bonus: 10 }; let key = "bonus"; delete zap____key____; console.log(____ in zap);
Options: [ ] key . "key"
Hint : A variable key needs the bracket notation, and the check must use the variable's value, not the word key.
Exercise 8 — delete Cheat Sheet
Match each line with its effect.
Items to match
• delete obj.a
• obj.a = undefined
• "a" in obj
• delete on a missing key
Definitions
• Removes the property a
• Keeps the key, without a value
• true if the key a exists
• Nothing happens
Exercise 9 — Quiz: The Right Way
Which line removes the `type` property from `zap`?
Options
• ```delete zap.type;```
• ```zap.type = "";```
• ```remove zap.type;```
• ```zap.delete("type");```
Hint : Removing is done with a keyword, not a method, and an empty value is still a value.
Exercise 10 — End of Battle Cleanup
The battle is over, clean up Zap's temporary properties. 1. Remove `status` and `bonus` 2. Add a property `ready` worth `true` 3. Print `"Status still there? X"` and `"Bonus still there? Y"` using the operator that checks whether a key exists 4. Print `"Ready: Z"`
Starter code
const zap = { name: "Zap", level: 4, status: "paralyzed", bonus: 15 }; // Write your code here
Back to the course