Objects as Maps
Using a plain object as a dictionary means treating property names as keys. It works for fixed string keys and fails when keys are user input, numbers you care about, or names that collide with inherited properties.
Detailed Explanation
Before Map existed, every dictionary in JavaScript was an object. { ada: 3, bram: 1 } is still a fine table when you control the keys. counts[name] += 1 is short. JSON.stringify works. For a record with known fields, an object is the right shape.
The problems appear when the set of keys is not a safe, closed list.
Keys are strings or symbols. obj[1] stores "1". obj[{ id: 1 }] stores "[object Object]", and a second object key overwrites the first. You cannot use objects, functions, or NaN as real keys.
Inherited names are visible. "toString" in {} is true. obj["constructor"] is Object. If name comes from a query string or JSON and you write obj[name] = 1, a key __proto__ historically mutated the prototype (this is largely closed now via Object.prototype being immutable and __proto__ assignment being special-cased, but constructor and toString still shadow). for...in walks inherited enumerable keys. Object.keys does not, but in and direct reads still see the prototype.
Object.create(null) builds an object with no prototype. It has no toString, no hasOwnProperty. It is a clean string-to-value table. You lose obj.toString() and must use Object.prototype.toString.call(obj). This is the right object-as-map when you refuse to add a Map dependency in very old environments. In modern code, Map is clearer.
Object.hasOwn(obj, key) (ES2022) tests own keys without touching the prototype. Use it instead of obj.hasOwnProperty(key), which fails if hasOwnProperty was overwritten or if obj is Object.create(null).
What it costs you: a class of security and correctness bugs called prototype pollution when untrusted keys write onto objects that still have a prototype. Even without pollution, if (obj[key]) is wrong for stored 0 and for inherited truthy names.
Reach for a plain object when the keys are identifiers you wrote. Reach for Object.create(null) only when you must stay on objects and keys are dynamic. Reach for Map for dynamic keys, non-string keys, frequent deletes, or insertion-order iteration that includes numeric-looking keys without sorting them.
Code Examples
const scores = { ada: 10, bram: 7 };
console.log(scores.ada); // => 10
console.log(Object.keys(scores)); // => [ 'ada', 'bram' ]const table = {};
table[1] = "one";
console.log(Object.keys(table)); // => [ '1' ]
console.log("toString" in table); // => true
console.log(Object.hasOwn(table, "toString")); // => false
const clean = Object.create(null);
clean[1] = "one";
console.log("toString" in clean); // => false
console.log(clean[1]); // => onefunction increment(counts, key) {
const safe = Object.hasOwn(counts, key) ? counts[key] : 0;
counts[key] = safe + 1;
}
const counts = Object.create(null);
increment(counts, "constructor");
console.log(counts.constructor); // => 1Common Gotchas
Dynamic keys on a normal object can clash with inherited names
`constructor`, `toString`, `__proto__`. Use `Map` or `Object.create(null)` plus `Object.hasOwn`.
Numeric keys stringify and sort
`{ 2: "b", 1: "a" }` enumerates `1` then `2`. A `Map` keeps insertion order.
Object keys cannot be objects
they become `"[object Object]"`. Use a `Map`.
`obj.hasOwnProperty` is not safe on all objects
it is missing on `Object.create(null)` and can be overwritten. Prefer `Object.hasOwn`.