Maps in JavaScript are an object type that allow you to store collections in key-value pairs. You can think of how a dictionary works in the sense that if you want to know the definition of a word (the value), you only need to locate the word itself (the key).
Use maps when you want to store key-value pairs.
Creating a Map
Creating a map in JavaScript is the same as creating any object; using the new keyword.
JAVASCRIPTconst map = new Map();
With your map defined, you can move on to adding elements to it.
Set
Adding an element to a map is done by setting an element. The idea here is that you're "setting" an entry into your map. The syntax is:
JAVASCRIPTconst map = new Map();
map.set("red", "apple");
map.set("blue", "blueberry");
map.set("green", "pear");
console.log(map);
JAVASCRIPT0: {"red" => "apple"}
1: {"blue" => "blueberry"}
2: {"green" => "pear"}
Keep in mind that there can only be a single value assigned to each key. If you set red to cherry, the old value of apple is lost. Let's see what happens if you try to set red to cherry.
JAVASCRIPTconst map = new Map();
map.set("red", "apple");
map.set("red", "cherry");
console.log(map);
JAVASCRIPT0: {"red" => "cherry"}
The value of red is now cherry instead of apple.
Get
After you have populated a map with some elements, you can get them back by using the get method and passing in the key. The value that is returned is the value that was set for that key.
JAVASCRIPTconst map = new Map();
map.set("red", "apple");
map.set("blue", "blueberry");
map.set("green", "pear");
const item = map.get("blue");
console.log(item);
JAVASCRIPTblueberry
Great, we got back the value for the given key.
Size
Getting the size of a map is as simple as just using the size property. The size of a map is the number of elements that are in the map. The following example shows how to get the size of a map:
JAVASCRIPTconst map = new Map();
map.set("red", "apple");
map.set("blue", "blueberry");
map.set("green", "pear");
console.log(map.size);
JAVASCRIPT3
Has
Instead of getting an element and then checking if it is null or even if it exists at all, you can use the has method to check if a map has a key and it returns true if it exists and false if not. The following example shows how to use the has method:
JAVASCRIPTconst map = new Map();
map.set("red", "apple");
map.set("blue", "blueberry");
map.set("green", "pear");
console.log(map.has("red"));
console.log(map.has("yellow"));
JAVASCRIPTtrue
false
Delete
To get rid of an element in a map after it has been set, call the delete method on it and pass in the key. The following example shows how to use the delete method:
JAVASCRIPTconst map = new Map();
map.set("red", "apple");
map.set("blue", "blueberry");
map.set("green", "pear");
map.delete("blue");
console.log(map);
JAVASCRIPT0: {"red" => "apple"}
1: {"green" => "pear"}
With blue deleted, only red and green exist.
Clear
Clearing a map will delete all elements inside it, resetting it to a state as if it were a brand-new map. The following example shows how to use the clear method:
JAVASCRIPTconst map = new Map();
map.set("red", "apple");
map.set("blue", "blueberry");
map.set("green", "pear");
map.clear();
console.log(map);
JAVASCRIPT{}
Use maps when you want to store key-value pairs and you want to be able to get the value back from a key.
Resources
How to Install Node on Windows, macOS and Linux
Getting Started with Solid
Managing PHP Dependencies with Composer
Getting Started with Svelte
How to deploy a .NET app using Docker
How to build a Discord bot using TypeScript
How to deploy a Deno app using Docker
Getting Started with Sass
Learn how to use v-model with a custom Vue component
Getting User Location using JavaScript's Geolocation API
Learn how to build a Slack Bot using Node.js
Using Push.js to Display Web Browser Notifications
