JavaScript
This page explains JavaScript Map, a collection of key-value pairs that keeps insertion order.
Goal: understand Map keys and values, then use set(), get(), has(), and delete() correctly.
Map
A Map associates a key with a value. Unlike ordinary object properties, Map keys can be values of many types, including numbers and objects. Entries are iterated in insertion order.
Basic operations
JavaScript
const scores = new Map();
scores.set("Aki", 80);
scores.set("Ren", 95);
console.log(scores.get("Aki")); // 80
console.log(scores.has("Ren")); // true
console.log(scores.size); // 2
scores.delete("Aki");set(key, value)adds or replaces an entry.get(key)returns the value for a key, orundefinedif it is missing.has(key)checks whether a key exists.delete(key)removes one entry.clear()removes every entry.
Object keys are allowed
JavaScript
const settings = new Map();
const user = { id: 1 };
settings.set(user, { theme: "dark" });
console.log(settings.get(user).theme); // darkMap compares object keys by identity. The exact same object reference must be used to retrieve the entry.
Map or object?
Use a Map when keys can be arbitrary values, insertion order matters, or frequent key-based updates are central to the design. Use an ordinary object when you are modeling a simple record with known string keys and JSON-like data.