A dictionary is a data structure that you can use to store data in your application. You can store data using a key-value pair, which allows you to look up and retrieve a specific value.

Once you have stored data in a dictionary, you can complete other actions such as iterating over each item. You can also check if an item exists, or delete an item that is no longer needed.

How to Create a Dictionary Object

A dictionary is one of the many important data structures that you can use to store data. You can create dictionaries in C# and many other programming languages. You can also create an equivalent hashmap data structure in Java.

There is no "dictionary" keyword that you can use to create a dictionary object in JavaScript. However, you can create a dictionary using a generic object. Here's an example of how you can create an empty dictionary using the "Object" keyword:

        let dictionary = new Object();
    

You can also create an empty dictionary using this shorthand syntax:

        let emptyDictionary = {};
    

If you would like to initialize the dictionary with values, you can add each value in the format of "key : value".

Using the example below, you can create a string key called "Pidgey", and associate it with a value. The value is an object with properties for the pet's age, color, and gender.

        let petDictionary = {
    "Pidgey": { Age: 0.5, Color: "Gray", Gender: "Male" },
    "Mocha": { Age: 0.5, Color: "Brown", Gender: "Female" },
};

Keys are not restricted to string data types. You can use other data types such as numbers or boolean values.

        let wcDictionary = { 
   1: { Team: "Argentina" },
   2: { Team: "France" },
};

let dictBool = {
   true: { Message: "Confirmed" },
   false: { Message: "Denied" },
};

How to Add Values to the Dictionary Object

You can add new items to a dictionary using this format:

        dictionary[new_key] = new_value
    

The new_key can be any valid key value of your choice. This is the key you will use later on when you want to access that specific item in the dictionary. The new_value can be any object or value that you want to associate with the key.

This is an example of how you can add a new item to a dictionary using some example values:

        petDictionary["Apples"] = { Age: 2, Color: "Green", Gender: "Male" };
    

Just like when initializing, you can also use other data types to represent the key:

        wcDictionary[3] = { Team: "Morocco" };
    

How to Access Values Based on a Key

You can access a value from a dictionary using its key value:

        let dictionaryValue = petDictionary["Mocha"];
console.log(dictionaryValue);

The value returned will contain the entire object or value stored for that key:

Console log printing value of dictionary item

How to Iterate Over Each Item in the Dictionary

You can iterate over each item in the dictionary using the Object.keys() method. The Object.Keys() method returns an array that contains all the keys used in the dictionary:

        console.log(Object.keys(petDictionary));
    

In your console, you should then see an array containing all the dictionary’s keys:

Console log printing keys in dictionary

You can use the list of keys to loop through each item in the dictionary, and retrieve the value for each key:

        for (const key of Object.keys(petDictionary)) { 
   console.log(key + ": ");
   console.log(petDictionary[key]);
};

With the following results in your console:

Console log printing all items in dictionary

How to Check if an Item Exists in the Dictionary

You can check if a key exists in the dictionary using the "in" keyword:

        let inDictionary = 'Mocha' in petDictionary;  // returns true
let notInDictionary = 'a' in petDictionary; // returns false

You can also use the hasOwnProperty() method to check if an item exists:

        let exists = petDictionary.hasOwnProperty('Mocha');  // returns true
let doesNotExist = petDictionary.hasOwnProperty('a'); // returns false

How to Delete a Value From the Dictionary

You can set an item to null to indicate it has no value:

        petDictionary['Apples'] = null;
    

That item, however, will still be present in the dictionary. If you want to remove the item altogether, you can delete it using the "delete" keyword:

        delete petDictionary['Apples'];
    

Storing Data Inside Dictionaries in JavaScript

JavaScript doesn’t have first-class support for dictionaries, but you can use a plain Object to store key/value pairs.

A dictionary is a very powerful data structure that you can use to store and access data using keys. A dictionary isn't the only place where you can store data, so you can explore other data structures that could better suit your use case.