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 instance
1const map = new Map();
// set() adds key-value pairs
2map.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 key
5console.log(map.get("name"));
// has() checks if key exists
6console.log(map.has("age"));
// size property returns number of entries
7console.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 key
Practice typing this code

Practice 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

More JavaScript examples at level 4

← JavaScript typing practice