

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Maps
incomplete
2: Map Keys
incomplete
3: Map vs. Object
incomplete
This lesson's interactive features are locked, please to keep using them
JavaScript also offers (as of recently) support for maps: collections of key-value pairs. Map keys are unique, so adding a key that already exists will replace its value. You can set and delete entries dynamically:
const map = new Map();
map.set("bertholdt", "cadet");
map.set("reiner", "cadet");
map.set("annie", "cadet");
map.set("bertholdt", "soldier");
console.log(map);
// Map { 'bertholdt' => 'soldier', 'reiner' => 'cadet', 'annie' => 'cadet' }
map.delete("annie");
console.log(map);
// Map { 'bertholdt' => 'soldier', 'reiner' => 'cadet' }
Maps can be constructed from any iterable. Maps are iterable, so that means the Map constructor can accept a map:
const originalMap = new Map();
originalMap.set("bertholdt", "cadet");
const mapCopy = new Map(originalMap);
console.log(mapCopy);
// Map { 'bertholdt' => 'cadet' }
Textio is building a phone directory for its support team. Complete the addToPhonebook function. It takes a phoneNumber and name strings, and phoneBook map as inputs and returns a new map.