Menu

1 / 2

Map and Set in JavaScript

DEV Community·Pratham·6 months ago
#iur7K8bG
#when#part#creating#visual#const
Reading 0:00
15s threshold

Two data structures that solve problems objects and arrays can't — and when you should reach for them.

For a while, I thought JavaScript only had two ways to store data: objects (key-value pairs) and arrays (ordered lists). They handle most situations, so I never questioned whether there was something better.

Then I ran into two problems that made me realize their limitations:

Problem 1: I needed an object where the keys were other objects , not just strings. Regular objects can't do that — every key gets converted to a string.

Problem 2: I needed an array with no duplicates. I kept adding values and then manually checking "wait, is this already in there?" before each insertion.

Both problems have clean solutions: Map and Set .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.