(click anywhere to close)

[JavaScript] Keyed Collections

category: Website | course: Advanced Javascript | difficulty:

Until now, Objects have been somewhat regarded as the biggest collections within JavaScript. With the latest version, however, the new keyed collections Maps and Sets have been introduced, along with their siblings WeakMaps and WeakSets


Maps are used to map values to values. A map can, unlike objects, link any key to any value. They are created using the new Map() constructor.

var someMap = new Map();

To set and get values, use the set(key, value) and get(key) methods. To completely delete a key, use the delete(key) method. To delete all keys at once, use clear().

var someMap = new Map();
console.log(someMap.get(true)); //Prints 15
console.log(someMap); //Prints Map {}, an empty map

If you want to check whether a map has a certain key, use the has(key) method. To retrieve the size of the map, use the size property.

var someMap = new Map();
someMap.set("Juan","Cool Guy");
var x = someMap.has("Juan"); //x is true
x = someMap.size; //x is 1


WeakMaps are the same as maps, but with two big differences. Firstly, it can only use Objects as the keys. Secondly, if there is no other reference left in the program to such Object, it is removed from memory, and thus also from the WeakMap.

var map = new WeakMap();


Sets, created with new Set(), are collections of values, much like arrays. What makes them different, is that they have two important properties from sets as defined in mathematics:

  • Every value may only occur once
  • All values are in no particular order

Because of this uniqueness, you can delete elements by value using the delete(value) method. You can add values to the set with the add(value) method. But, because the order isn’t defined, you can’t access one particular element – you have to loop through them all, in insertion order, to access them. Nevertheless, just like Maps, it also has the has(value) and clear() methods, and the size property.

var someSet = new Set();
console.log(someSet); //Prints Set {1}

//Or create a set from an array
var someSet = new Set([1,4,5,6,4,5,1]); //someSet is Set {1,4,5,6}


WeakSets are the same as sets, but with the same two big differences as between Map and WeakMap. If no other reference to an object within the set is present, the object is removed from the program. There is, therefore, no size property.

var weakSet = new WeakSet();


You must iterate over Maps and Sets using a for … of loop. This happens in the order of insertion.

var someSet = new Set([1,4,7,8]);

for(var value of someSet) {

/* Prints

The forEach(callback, thisObject) method, which you know from Arrays, is also available for both Maps and Sets.

On top of that, there are three methods that return a list of what’s inside such a map or set.

  • The keys() method returns all keys. For sets, which only collect values, this is the same as the next method.
  • The values() method returns all values.
  • The entries() method returns all key/value pairs.

The lists they return are not Arrays – actually, they are not of a type you’ve ever seen before. They return iterators, which is what next chapter is all about.

Do you like my tutorials?
To keep this site running, donate some motivational food!
Chocolate Milk