Map
In one line
A Map is a collection of key-value pairs where keys can be of any type, maintaining insertion order.
In simple words
A Map is a data structure that holds key-value pairs, similar to an object, but with important advantages: keys can be any type (not just strings/symbols), it maintains insertion order, and it has a size property.
Maps are useful when you need to use non-string keys (objects, functions, numbers as actual numbers) or when you frequently add and remove entries. Methods include set, get, has, delete, clear, and iteration via forEach or for...of.
Unlike objects, Maps are designed for frequent addition and removal of entries and do not have a prototype chain with default properties. Use a Map when you need a proper key-value store; use an object when you need a fixed shape with named properties.
Example
const map = new Map();
map.set("name", "Ada");
map.set(42, "answer");
map.set(true, "yes");
console.log(map.get(42)); // "answer"
console.log(map.size); // 3
console.log(map.has("name")); // trueMaps accept any key type and maintain insertion order.
Related terms
Related Resources
Related Lessons
- Data Types
Learn about Map.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.