Map Data Structure in JavaScript

Level 4288 keystrokes

Map provides a key-value data structure with methods like set, get, has, delete and size property.

JavaScript code

// Create a new Map for user roles
1const userRoles = new Map();
// set() adds a key-value pair: user name → role
2userRoles.set("alice", "admin");
3userRoles.set("bob", "editor");
4userRoles.set("charlie", "viewer");
// get() returns the value for a key: admin
5console.log(userRoles.get("alice"));
// has() checks whether a key exists: true
6console.log(userRoles.has("bob"));
// size is the number of entries: 3
7console.log(userRoles.size);
// delete() removes the entry for a key
8userRoles.delete("charlie");
// Size is now 2
9console.log(userRoles.size);

Output

admin
true
3
2
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.

  • "× 18
  • .× 12
  • (× 11
  • )× 11
  • ;× 9
  • ,× 3
  • =× 1

More JavaScript examples at level 4

← JavaScript typing practice