1. Create a Map
new Map() builds an empty Map that keeps keys in insertion order.
const counts = new Map()Typorithm · medium
Step 1 of 8 · intro card
Objects & Maps
A Map stores key/value pairs with map.set, map.get, map.has, and map.size. Prefer a Map over a plain object when keys are not strings, when insertion order matters, or when you add and delete entries frequently.
A Map is a dedicated key/value collection: set(key, val) writes, get(key) reads, has(key) tests membership, and size reports the count directly.
Unlike a plain object, a Map accepts any value as a key — objects, functions, numbers — and never collides with prototype keys like __proto__ or toString.
Maps preserve insertion order, expose size in O(1), and are optimized for frequent additions and deletions, so reach for a Map when the object you would otherwise use is really a lookup table.
set / get / has / size are the core Map operations.
Map keys can be any value, not just strings.
Maps keep insertion order and report size directly.
const counts = new Map()
counts.set("ada", 1)
counts.set("ada", counts.get("ada") + 1)
const seen = counts.has("ada")
const total = counts.size
const objKey = new Map()
objKey.set({ id: 1 }, "first")Connect each part of the code to the concept before you type.
new Map() builds an empty Map that keeps keys in insertion order.
const counts = new Map()set(key, value) stores or overwrites the value for a key and returns the Map.
counts.set("ada", 1)get(key) returns the stored value, or undefined when the key is absent.
counts.get("ada")has(key) returns a boolean, the safe way to test membership.
counts.has("ada")size is the entry count, unlike a plain object which needs Object.keys(obj).length.
counts.sizeA Map accepts any value as a key, including objects — a plain object cannot.
objKey.set({ id: 1 }, "first")Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.