Map Data Structure in JavaScript
Level 4227 keystrokes
Map is a key-value collection that allows any type as keys, unlike plain objects.
JavaScript code
// Create a new Map instance1const map = new Map();// set() adds key-value pairs2map.set("name", "Alice");3map.set("age", 25);// Objects can be used as keys (not possible with plain objects)4map.set({ key: 1 }, "object key");// get() retrieves value by key5console.log(map.get("name"));// has() checks if key exists6console.log(map.has("age"));// size property returns number of entries7console.log(map.size);// forEach iterates over entries (value, key order)8map.forEach((v, k) => console.log(k, v));Output
Alice
true
3
name Alice
age 25
{ key: 1 } object keyPractice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this JavaScript code and how often they appear.
"× 12(× 12)× 12.× 11;× 8,× 5=× 2:× 1{× 1}× 1