Any Value as a Key
What you'll learn: How Maps let you use any JavaScript value as a key—not just strings and symbols like plain objects.
The Limitation of Plain Objects
When you use a plain object as a key-value store, JavaScript automatically converts keys to strings:
const obj = {};
const keyObj = { id: 1 };
const keyFunc = function() {};
obj[keyObj] = "value1";
obj[keyFunc] = "value2";
console.log(Object.keys(obj));
// ["[object Object]", "function() {}"]
Notice how the object and function became strings? That's a huge limitation—two different objects both become "[object Object]", causing collisions.
Maps Accept Any Key Type
Maps break this limitation. You can use objects, functions, numbers, strings, symbols, or even undefined as keys, and each remains distinct:
const map = new Map();
const objKey = { id: 1 };
const funcKey = function() {};
const numKey = 42;
map.set(objKey, "stored with object key");
map.set(funcKey, "stored with function key");
map.set(numKey, "stored with number key");
console.log(map.get(objKey)); // "stored with object key"
console.log(map.get(funcKey)); // "stored with function key"
console.log(map.get(numKey)); // "stored with number key"
Each key is compared by reference (for objects/functions) or value (for primitives), not converted to a string.
Why This Matters
This flexibility means you can associate data with actual objects in your program—like mapping DOM elements to metadata, or functions to configuration—without workarounds.
Key Takeaway: Unlike plain objects which convert keys to strings, Maps preserve any value type as a key, enabling you to use objects, functions, and primitives without collision.