DEV Community

loading...
Cover image for Map and Set in JavaScript for Humans

Map and Set in JavaScript for Humans

obetomuniz profile image Beto Muniz ใƒป1 min read

Would you like to hear more about Set and Map objects in JavaScript?

Let's catch up with them on this thread ๐Ÿงต


Set in JavaScript ๐Ÿ‘‡

๐Ÿ“Œ It's not key/value like the Object type. Keys are just values exposed
๐Ÿ“Œ Don't accept duplicated values
๐Ÿ“Œ It's iterable with forEarch method and for...of
๐Ÿ“Œ Conceptually is similar to []

๐Ÿ’ก Set is useful to remove duplicates on [].


Map in JavaScript ๐Ÿ‘‡

๐Ÿ“Œ Allow/preserve any key type. Even objects
๐Ÿ“Œ Don't expose insecure data properties
๐Ÿ“Œ Iterate with forEarch method and for...of
๐Ÿ“Œ Stay the insertion order doesn't matter the type

๐Ÿ’ก Prefer Map instead of {} for client-side data manipulation.


Thoughts on Map, Set, {}, and [] in JavaScript ๐Ÿ‘‡

๐Ÿ“Œ Each has a specific usage in JS
๐Ÿ“Œ Use {} for data traffic from the server.
๐Ÿ“Œ Set helps apply omit, diff, etc. on []
๐Ÿ“Œ Map return size. {} don't return size

๐Ÿ—ฃ There's not a silver bullet. Use them carefully.


๐Ÿ˜‰ Hope that you now understand better such useful objects on JavaScript, they are highly valuable, and master them will make you a better JavaScript Developer.

๐Ÿ’Œ Also, don't miss content like this signing my newsletter.

Discussion (0)

pic
Editor guide