Why delete a key
Sometimes an entry has to go: a shop stops selling an item, or a player leaves a game. In JavaScript the delete operator removes a key and its value from an object together. Write delete, then the object with the key in square brackets.
const animals = {"cat": 4, "dog": 2, "rabbit": 1};
delete animals["rabbit"];
console.log(Object.keys(animals).length);
console.log("rabbit" in animals);
console.log(animals);
2
false
{ cat: 4, dog: 2 }
The number of keys dropped by 1, and the key is gone, so "rabbit" in animals is now false. The method Object.keys(animals) gives an array of the keys, and its length counts them.
Details
A missing key is not an error
Deleting a key that is not there does nothing. The expression delete animals["zebra"] gives no error and leaves the object as it was.
undefined is not deletion
Setting a value to undefined does not delete its key. The key stays, and Object.keys still counts it:
const b = {"cat": 4, "rabbit": 1};
b["rabbit"] = undefined;
console.log(Object.keys(b).length);
console.log("rabbit" in b);
2
true
If a program later asks whether the key exists, it would still hear yes. Only delete removes the key.
const objects can lose keys
The lesson's object is declared with const, and deleting still works. The word const only stops you from giving the name animals a new object. It does not freeze the object's contents. Trying animals = {} would fail with Assignment to constant variable.
Dot form
delete animals.rabbit does the same as delete animals["rabbit"] when the key is a simple name. The bracket form also works for keys with spaces or keys held in a variable.
Thinking about the entries
An object is a set of entries, and each entry is a key with its value. Deleting works on whole entries. You cannot delete only the value and keep the key, and you cannot delete the key and keep the value. If you want an entry that says "none", store a real value such as 0 or an empty string, and keep the key. If you want the entry gone, as when the last rabbit has been sold, use delete.
That difference shows up when you count. The number of keys is the number of entries. A shop that lists "rabbit: 0" still lists rabbits, and a program that loops over the keys would show them. Which one you want depends on the story, and in this task the story says the rabbit is gone for good.
Deleting also changes what a later check sees. Before the delete, "rabbit" in animals is true. After it, the same check is false. The code at the bottom of the starter file relies on exactly that, and it is how you can tell that the delete worked, apart from counting the keys.
The lesson's task
A pet shop counts what it has for sale in animals. The last rabbit just went home with a family, so the key "rabbit" has to go. The starter code prints the number of keys once, and at the bottom it checks whether any rabbits are left. The job is to delete the key and print the number of keys again.
- Below the first
console.log, delete the key"rabbit"with thedeleteoperator. - On the next line, print
Object.keys(animals).lengthagain.
const animals = {"cat": 4, "dog": 2, "rabbit": 1};
console.log(Object.keys(animals).length);
delete animals["rabbit"];
console.log(Object.keys(animals).length);
if ("rabbit" in animals) {
console.log("We still have rabbits");
} else {
console.log("No rabbits left");
}
3
2
No rabbits left
The first number is 3, before the sale. After the delete it is 2, and the check at the bottom finds no "rabbit" key.
Key takeaways
delete obj[key]removes the key and its value together.- Deleting a missing key is harmless.
- Setting a value to
undefinedkeeps the key. - A
constobject can still lose keys.
