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.

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.

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