Tutorial by Examples

A Map is a basic mapping of keys to values. Maps are different from objects in that their keys can be anything (primitive values as well as objects), not just strings and symbols. Iteration over Maps is also always done in the order the items were inserted into the Map, whereas the order is undefine...
To remove all elements from a Map, use the .clear() method: map.clear(); Example: const map = new Map([[1, 2], [3, 4]]); console.log(map.size); // 2 map.clear(); console.log(map.size); // 0 console.log(map.get(1)); // undefined
To remove an element from a map use the .delete() method. map.delete(key); Example: const map = new Map([[1, 2], [3, 4]]); console.log(map.get(3)); // 4 map.delete(3); console.log(map.get(3)); // undefined This method returns true if the element existed and has been removed, otherwise fal...
To check if a key exists in a Map, use the .has() method: map.has(key); Example: const map = new Map([[1, 2], [3, 4]]); console.log(map.has(1)); // true console.log(map.has(2)); // false
Map has three methods which returns iterators: .keys(), .values() and .entries(). .entries() is the default Map iterator, and contains [key, value] pairs. const map = new Map([[1, 2], [3, 4]]); for (const [key, value] of map) { console.log(`key: ${key}, value: ${value}`); // logs: // ke...
Use .get(key) to get value by key and .set(key, value) to assign a value to a key. If the element with the specified key doesn't exist in the map, .get() returns undefined. .set() method returns the map object, so you can chain .set() calls. const map = new Map(); console.log(map.get(1)); // und...
To get the numbers of elements of a Map, use the .size property: const map = new Map([[1, 2], [3, 4]]); console.log(map.size); // 2

Page 1 of 1