JavaScript Getting and setting elements


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)); // undefined
map.set(1, 2).set(3, 4);
console.log(map.get(1)); // 2