Start learning free

LESSON 128 OF 671 · DICTIONARIES

Deleting Dictionary Keys in JavaScript: the delete Operator

Lesson Read the full explanation

TL ; DR:

The delete operator removes a key and its value from an object. The object has one entry fewer.


const toys = {"ball": 3, "doll": 2, "kite": 1};

delete toys["kite"];

console.log(Object.keys(toys).length);   // 2


Full lesson:

Sometimes an entry has to go. A shop stops selling an item. A player leaves the game.

Write delete, then the object with the key in square brackets. The key and its value go away together.

The number of keys drops by 1. The key is gone, so "kite" in toys is now false.

Deleting a key that isn't there does nothing. There is no error.

Watch out: setting a value to undefined doesn't delete its key. The key stays, and Object.keys still counts it.

A const object can still lose keys. The const only stops you from giving toys a new object.


Your task

A pet shop counts what it has for sale in animals. Each key is a kind of animal. Its value is how many the shop has.

The last rabbit just went home with a family. Delete the key "rabbit" from animals.

Then print the number of keys in animals again. The code at the bottom checks if any rabbits are left.

Expected output:

3
2
No rabbits left
STEP 1 OF 2
    Ctrl + Enter to check

    JAVASCRIPT · DICTIONARIES

    Deleting Dictionary Keys in JavaScript, explained

    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.

    1. Below the first console.log, delete the key "rabbit" with the delete operator.
    2. On the next line, print Object.keys(animals).length again.
    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 undefined keeps the key.
    • A const object can still lose keys.
    Keep going with JavaScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free