WeakMap is one of the most underrated and least used data structures in JavaScript. There are many JavaScript developers who don’t even know they exist. This tutorial will help you understand them. You will learn about what WeakMaps are, how they work and how they differ from Maps.

A quick introduction to WeakMap

Both, Maps and WeakMaps are new data structures, or collections, introduced in [ES6]. Another example of a collection is an array. Similarly to an array, both Maps and WeakMaps allow you to store data. In case of these Maps and WeakMaps you store in the form of key-value pairs.

If you want to access some value stored in a Map, all you have to do is to use the correct key. This also works for WeakMaps. When you want to access some value stored in a WeakMap, you also have to use correct key. Both, Maps and WeakMaps allow you to add new key-value pairs and remove existing.

What if you are not sure if a Map or WeakMap contains specific key? There is a method you can use to quickly check if the key exists. So, this is where Maps and WeakMaps work in the same way. Along with these similarities, there are some important differences between these two you need to know.

#javascript #weakmap

WeakMap in JavaScript - An Easy Introduction
1.25 GEEK