Skip to main content

Command Palette

Search for a command to run...

Mastering Map and Set in JavaScript

Updated
3 min read
Mastering Map and Set in JavaScript

JavaScript is full of interesting structures, and Map and Set are two that often fly under the radar. They can make your life easier when dealing with collections of data. Let’s explore what these structures are and how to use them effectively.

What Map is

Map is a collection of key-value pairs where keys can be of any type. Unlike regular objects, which only accept strings as keys, a Map can take anything from numbers to objects. Imagine you’re tracking players in a cricket match. You could use a Map to associate each player’s name with their score. Here’s how it looks:

let playerScores = new Map();
playerScores.set('Sachin Tendulkar', 100);
playerScores.set('Virat Kohli', 150);

In this case, you can retrieve a player’s score by using their name as the key:

console.log(playerScores.get('Sachin Tendulkar'));

What Set is

Set is a collection that allows you to store unique values. No duplicates are allowed, which is perfect when you need to maintain a list of items without repetition. Picture a scenario where you want to store the jersey numbers of players on a football team. You can use a Set to keep track of unique numbers:

let jerseyNumbers = new Set();
jerseyNumbers.add(10);
jerseyNumbers.add(7);
jerseyNumbers.add(10);

When you try to add the same number again, the Set ignores it. You can check the size of the Set to confirm this:

console.log(jerseyNumbers.size);

Difference between Map and Object

While both Map and Object can hold key-value pairs, they serve different purposes. An Object is more suited for representing a single entity, like a person. For instance:

let ayushDetails = {
  name: 'Ayush',
  age: 25,
  location: 'Himachal Pradesh'
};

Objects are not iterable, but Maps are. When you need to maintain the order of elements or use keys of any type, Map shines. Here’s a quick comparison:

  • Objects use strings and Symbols as keys. Maps can use any type.

  • Maps remember the original insertion order of the keys. Objects don’t guarantee any order.

Difference between Set and Array

Arrays are indexed collections that allow duplicate items. Sets, on the other hand, only hold unique values. If you were creating a list of favorite movies, an Array would allow you to add duplicates:

let favoriteMovies = ['Inception', 'The Dark Knight', 'Inception'];

If you want to keep it unique, a Set would do the job:

let uniqueMovies = new Set();
uniqueMovies.add('Inception');
uniqueMovies.add('The Dark Knight');
uniqueMovies.add('Inception');

The Set won’t let ‘Inception’ appear twice. This makes Sets a good choice for maintaining lists of unique items, like usernames in a gaming app.

When to use Map and Set

Use Map when you need a collection of key-value pairs with keys of any type. It’s great for scenarios like caching data or tracking user sessions. Use Set when you want to ensure all values are unique. This is useful for scenarios like keeping track of unique user IDs in a registration system.

In conclusion, both Map and Set have their strengths. They provide powerful ways to manage collections in JavaScript, and knowing when to use them can simplify your code. Get comfortable with these structures, and you’ll find them invaluable in your development toolkit.