JS Maps
const mapName = new Map();
mapName.set(key, value);
mapName.get(key);
mapName.has(key);
Creating a Map
A Map is created with new Map(), optionally initialized from an array of [key, value] pairs, giving you a structured collection ready for lookups by key from the start.
Example: Creating a Map
const map = new Map([["name", "Sam"], ["age", 30]]);
console.log(map);
set and get
set(key, value) adds or updates an entry in the Map, and get(key) retrieves the value stored under a given key, returning undefined if that key isn't present.
Both methods work with any value as a key, including objects and functions, not just strings.
Example: set and get
// Declare the constant `map` as a new `Map` instance
// Declare the constant `map` as a new `Map` instance
const map = new Map();
// Call `map.set("name", "Alex")`
// Call `map.set("name", "Alex")`
map.set("name", "Alex");
// Print `map.get("name")` to the console
// Print `map.get("name")` to the console
console.log(map.get("name"));
// Print `map.get("missing")` to the console
// Print `map.get("missing")` to the console
console.log(map.get("missing"));
has, delete, and size
has(key) checks whether a Map contains a specific key, delete(key) removes an entry entirely, and the size property reports how many entries the Map currently holds.
Checking has() before get() is a common way to distinguish a genuinely missing key from one whose value happens to be undefined.
Example: has, delete, and size
// Declare the constant `map` as a new `Map` instance
// Declare the constant `map` as a new `Map` instance
const map = new Map([["a", 1]]);
// Print `map.has("a")` to the console
// Print `map.has("a")` to the console
console.log(map.has("a"));
// Call `map.delete("a")`
// Call `map.delete("a")`
map.delete("a");
// Print `map.size` to the console
// Print `map.size` to the console
console.log(map.size);
Iterating a Map
Maps are iterable, and for...of paired with array destructuring, for (let [key, value] of myMap), is the most common and readable way to visit every key-value pair in insertion order.
Example: Iterating a Map
// Declare the constant `map` as a new `Map` instance
// Declare the constant `map` as a new `Map` instance
const map = new Map([["a", 1], ["b", 2]]);
// Classic for-loop: `let [key, value] of map`
// Classic for-loop: `let [key, value] of map`
for (let [key, value] of map) {
// Print `key, value` to the console
// Print `key, value` to the console
console.log(key, value);
}
Map vs Object
A Map allows any type of key, tracks its size directly, and maintains reliable insertion order, while a plain object is often simpler for straightforward string-keyed data and benefits from familiar dot and bracket notation.
- Reach for a Map when:
- keys aren't simple strings
- you need frequent additions and deletions
- you want a built-in size property.
Example: Map vs Object
// Declare the constant `map` as a new `Map` instance
// Declare the constant `map` as a new `Map` instance
const map = new Map();
// Call `map.set(1, "numeric key")`
// Call `map.set(1, "numeric key")`
map.set(1, "numeric key");
// Print `map.size` to the console
// Print `map.size` to the console
console.log(map.size);
// Declare the constant `obj`, set to `{ name: "Sam" }`
// Declare the constant `obj`, set to `{ name: "Sam" }`
const obj = { name: "Sam" };
// Print `obj.name` to the console
// Print `obj.name` to the console
console.log(obj.name);
- Using bracket notation, like myMap[key], instead of the proper get() and set() methods, Maps don't support bracket access.
- Forgetting that a Map's size is checked with the size property, similar to a Set, not length like an array.
- Confusing Map with a plain object, Maps allow any type of key, including objects and numbers, while plain object keys are always converted to strings.
- A Map stores key-value pairs where keys can be any type, not just strings.
- set(), get(), delete(), and has() are the primary methods for managing a Map's contents.
- Maps can be iterated with for...of, and their size property reports how many entries they contain.
Map has been supported in all major browsers since 2015 and is a fully standard part of modern JavaScript.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic