Does JavaScript Have Dictionaries? A Practical Guide
Explore how JavaScript implements dictionary-like structures with objects and Map, when to use each, and practical patterns for managing key–value data in real world applications.

Dictionaries in JavaScript are key-value stores implemented with objects or Map, used to map keys to values for fast lookup.
Does JavaScript Have Dictionaries?
Dictionaries are collections of key-value pairs used for fast lookups. In JavaScript there is no separate dictionary type; instead you rely on objects or the Map class. Conceptually, a dictionary maps a key to a value and allows quick retrieval by that key. This is why developers often refer to an object or a Map as a dictionary in everyday code. The practical takeaway is that dictionary-like structures are a core pattern in JavaScript, enabling you to model real world mappings such as user records, configuration settings, or caches.
How JavaScript Implements Dictionaries
JavaScript provides two primary dictionary implementations: plain objects and Map. An object is created with an object literal like {} and uses string keys by default, accessible via dot notation or bracket notation, e.g., obj.name or obj["name"]. A Map is created with new Map() and accepts any value as a key, with methods set, get, has, and delete. Maps also preserve insertion order, which can matter for iteration. For most simple dictionaries, an object is sufficient; for more advanced uses that require non-string keys or guaranteed order, Map is often a better fit. Keep in mind that objects carry a prototype, which can affect keys, while Maps do not have that issue.
Prototypical Pitfalls with Objects as Dictionaries
A plain object is a lightweight dictionary, but it inherits properties from the Object.prototype. This can lead to accidental keys or key collisions, or you might retrieve keys like toString that you did not intend to store. A common fix is to use Object.create(null) to create a dictionary with no prototype, and to check for keys with Object.prototype.hasOwnProperty.call(dict, key) rather than direct access. When iterating, prefer Object.keys(dict) or Object.entries(dict) to avoid surprises. If you plan to serialize to JSON, remember that nonstring keys will be converted to strings, which may not be what you expect.
The Map Alternative for Dictionary Like Data
If you need keys that are not strings or symbols, or you require reliable insertion order guarantees, Map is the better tool. Map keeps track of the number of entries via the size property and provides iterable interfaces for keys, values, and entries. You can chain operations, and Map can be cleared or reset efficiently.
Practical Dictionary Patterns in JavaScript
Here are patterns you can apply today:
- Simple dictionary with string keys using an object: const dict = { a: 1, b: 2 }; dict.c = 3; console.log(dict["a"]);
- Mutable dictionary using Object.create(null): const dict = Object.create(null); dict.x = 10;
- Dictionary with Map when non-string keys are needed: const map = new Map(); map.set({id:1}, 'obj');
- Iterating keys: Object.keys(dict) or map.forEach((v,k)=>...).
- Existence checks: k in dict or map.has(k).
Dictionaries and JSON: What You Need to Know
JSON is built around plain objects and arrays. When you stringify dictionaries backed by objects, keys become JSON object properties. If you use Map, default JSON.stringify does not include its entries; you will need to convert Map to an array of entries or an object before serialization. When you parse JSON back, you typically reconstruct the dictionary by iterating keys. Remember that all JSON object keys are strings.
Authority sources and best practices
Having a dictionary that is easy to maintain means choosing the right container for the job. For simple key-value lookups with string keys, a plain object is usually the best fit. If you require non-string keys, insertion order guarantees, or richer API surface, prefer Map. Always consider JSON interoperability when designing API payloads.
Questions & Answers
Can I use a plain object as a dictionary in JavaScript?
Yes. For simple key-value lookups with string keys, a plain object is a common, lightweight dictionary. Access keys with dot notation or bracket notation, and beware prototype keys. For JSON-friendly data, objects are typically the easiest choice.
Yes, you can use a plain object as a dictionary for simple key-value lookups with string keys.
What is the difference between Object and Map for dictionaries?
Objects are best for simple, JSON-friendly key-value data with string keys. Maps support any key type, preserve insertion order, and provide a richer API. Use Map when you need non-string keys or guaranteed consistent iteration order.
Objects are great for simple keys, while Map handles any key type and stable iteration.
Why would I use Object.create(null) for dictionaries?
Using Object.create(null) creates a dictionary with no prototype, avoiding inherited keys like toString. This makes dictionaries safer for arbitrary string keys and avoids collisions with built-in properties.
Object.create(null) gives you a clean dictionary without inherited properties.
Do dictionaries preserve insertion order in JavaScript?
Maps preserve insertion order by design. Plain objects preserve string-key order in modern engines, but it is not guaranteed in all edge cases; for predictable order, Map is the safer choice.
Yes, Maps preserve insertion order and are generally predictable for iteration.
Are maps JSON serializable?
By default, JSON.stringify does not serialize Map entries. Convert Map to an array of entries or to an object before serializing. Plain objects serialize directly to JSON.
Maps need conversion before you can serialize them to JSON.
How can I check if a key exists in a dictionary efficiently?
For objects, use the in operator or hasOwnProperty; for Map, use the has method. Choose the approach based on your container type and the keys you use.
Use in or hasOwnProperty for objects, has for Maps.
What to Remember
- Use objects for simple string-key dictionaries
- Prefer Map when nonstring keys or ordered iteration are needed
- Create dictionaries with Object.create(null) to avoid prototype keys
- Map preserves insertion order and supports any key type
- Serialize plain objects easily with JSON, Map requires conversion