DEV Community

zain ul abdin
zain ul abdin

Posted on

What Are JavaScript Sets and Maps?

In a vast variety of useful tools and features that JavaScript offers, powerful structures such as Sets and Maps often get overlooked.

What are they?

A Set is like a special list that is perfect when you need to ensure all your items are different.

On the other hand, a Map works a bit like a dictionary. Instead of words, you have keys, and instead of meanings, you have values.

A Set can be used to store names of people at a party without repetition, while a Map allows you to store people’s names and their favorite colors in a key-value pair.

Both Sets and Maps share some methods that make them easy to work with, such as set() to set a new value, get() to retrieve that value, and size to know the size of the structure.

However, when it comes to checking if something is a Set or a Map, it gets a little tricky, as using typeof returns both as objects. Instead, using instanceof will correctly tell you if your data is a Set or a Map.

Sets and Maps are powerful tools in JavaScript, allowing you to store and organize data in smart and efficient ways depending on your needs, and should be used more often.


To stay updated with more content related to web development and AI, feel free to follow me. Let's learn and grow together!

Top comments (0)