JavaScript › Map, Set, WeakMap & WeakSet

How does a Map differ from a plain object for storing key-value pairs?

Beginner

Answer

A Map allows keys of ANY type (objects, functions, even NaN), preserves insertion order reliably, has a .size property, and doesn't come with inherited prototype properties that could collide with your keys — plain objects only reliably support string/Symbol keys.
const map = new Map();
map.set('name', 'Sam');
map.set(42, 'answer');
console.log(map.get(42)); // 'answer'
console.log(map.size);   // 2
Real-world example Using an object instance as a key to associate metadata with it, which a plain object can't do.

Common follow-ups: Why might using an object as a plain-object key silently fail (via string coercion)?