Lesson 28 of 48
58%
JavaScript Maps
A Map stores key value pairs.
Unlike plain objects, keys can be any type.
Think of a locker wall. Each locker key can open one box.
1
Make a Map
2
set and get
3
has and delete
4
size
5
Loop through a Map
Create a Map
Use new Map().
javascript
set and get
set stores a value under a key.
get reads it back.
javascript
Any key type
Objects can use numbers as keys only after turning them into strings.
Maps keep the real key type.
javascript
has, delete, size
has checks for a key. delete removes a pair. size counts pairs.
javascript
Loop
You can loop keys, values, or entries.
javascript
Map vs object
Use a plain object for simple record shapes.
Use a Map when keys are weird types or you need frequent add and remove.
Tip: Remember get and set. Those two verbs unlock most Map work.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.