Introduction
Search traffic often uses phrases like javascript dictionary, dictionary in javascript,
js dictionary, or dict in javascript. In the language itself there is no separate
Dictionary type: those words usually mean a key-value store. The two standard
choices are a plain object ({} or new Object()) and the built-in Map
type (MDN keyed collections).
This article shows how to create and update both styles, when to prefer each, and examples you can run in Node or DevTools.
Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.
Quick reference: object vs Map
| Topic | Plain object | Map |
|---|---|---|
| Typical keys | Strings, symbols | Any value (including objects) |
| Size | Object.keys(o).length (not O(1)) |
map.size property |
| Iteration | for...in (own + inherited unless filtered), or Object.entries |
for...of directly |
| JSON | Natural fit with JSON.stringify |
Convert first (e.g. Object.fromEntries) |
| “Empty” object | {} still inherits Object.prototype |
No inherited keys |
Method 1: Object as a dictionary in JavaScript
Using the Object constructor
You can build a dictionary javascript style bag with new Object() and assign
keys with dot notation or bracket notation:
// Using dot notation
const myDictOne = new Object();
myDictOne.key1 = 'value1';
myDictOne.key2 = 'value2';
console.log(myDictOne);You should see one object log with key1 and key2 string values.
// Using bracket notation
const myDictTwo = new Object();
myDictTwo['key1'] = 'value1';
myDictTwo['key2'] = 'value2';
console.log(myDictTwo);You should see the same shape as the dot-notation example.
Accessing values
const myDictTwo = new Object();
myDictTwo.key1 = 'value1';
myDictTwo.key2 = 'value2';
const value1 = myDictTwo.key1;
const value2 = myDictTwo.key2;
console.log(value1, value2);You should see one line: value1 value2.
Updating a dict in JavaScript (add / change / delete)
const myDictTwo = new Object();
myDictTwo.key1 = 'value1';
myDictTwo.key2 = 'value2';
myDictTwo.key3 = 'value3';
myDictTwo.key2 = 'new value';
delete myDictTwo.key1;
console.log(myDictTwo);You should see an object with key2 set to new value and key3 unchanged; key1 is gone.
Prefer an object literal for new code
For most programs, {} is clearer than new Object() and matches common style guides.
See also our object literals article.
const lit = { a: 1, b: 2 };
console.log(lit);You should see { a: 1, b: 2 } (Node may print it with or without quotes on keys).
Numeric keys become strings on objects
If you treat a js dict as a plain object, remember that non-symbol keys are strings after property access rules apply:
const o = {};
o[1] = 'one';
console.log(Object.keys(o), JSON.stringify(o));You should see the keys array containing the string '1' and JSON {"1":"one"}.
Object.create(null) for dictionary-like bags
Ordinary objects inherit properties from Object.prototype. For dynamic string keys
(especially user input), a null-prototype object reduces surprises:
const safe = Object.create(null);
safe.foo = 1;
console.log('toString' in safe, Object.keys(safe));You should see false then ['foo']—no inherited toString shows up as an own key.
For arbitrary keys and frequent add/remove, a Map is often still the better tool.
Method 2: Map for a JavaScript dictionary
Map
stores key-value pairs where keys can be any type. Use set, get, has, and
delete. The entry count is map.size (a property, not a method—see
Map size).
Create and print a Map
const myDict = new Map();
myDict.set('key1', 'value1');
myDict.set('key2', 'value2');
console.log(myDict);You should see a Map summary with two string keys and their values (exact formatting varies by Node version).
Read values with get
const myDict = new Map();
myDict.set('key1', 'value1');
myDict.set('key2', 'value2');
const valueOne = myDict.get('key1');
const valueTwo = myDict.get('key2');
console.log(valueOne, valueTwo);You should see value1 value2.
size, has, and delete
const myDict = new Map();
myDict.set('key1', 'value1');
myDict.set('key2', 'value2');
console.log('size', myDict.size, 'has key1', myDict.has('key1'));
myDict.delete('key1');
console.log(myDict);
console.log('size', myDict.size);You should see size 2 and has key1 true, then a one-entry Map, then size 1.
Using non-string keys (objects as keys)
This is the main reason teams pick Map over a plain object for some dictionaries in javascript:
const o = {};
const mo = new Map();
mo.set(o, 42);
console.log(mo.get(o));You should see 42.
Iterating a Map
const m = new Map();
m.set('key2', 'value2');
for (const [k, v] of m) {
console.log(k, v);
}You should see one line: key2 value2.
Round-trip: Object.fromEntries
When keys are strings and you need a plain object (for example before JSON.stringify):
console.log(
JSON.stringify(
Object.fromEntries([
['x', 1],
['y', 2],
]),
),
);You should see {"x":1,"y":2}.
When to use which
- Plain object: JSON APIs, simple string-key records, object spread, and familiarity for most dictionary js tutorials and configs.
Map: non-string keys, frequent add/delete wheresizematters, ordered iteration tied to insertion, and a cleaner story for some security-sensitive key spaces (see MDN’sMapvsObjectdiscussion).
Summary
Objects fit JSON and string-key configs; Map fits arbitrary keys, stable size, and object references as keys—pick the smallest structure that matches how you read and serialize data.
A dictionary-style map in JavaScript is a pattern, not a separate language keyword: plain {} literals give you string keys with a prototype, Object.create(null) gives a bare map without inherited keys, and Map stores keys of any type with deterministic insertion order. Readers often ask when to pick Object versus Map; choose Map when keys are not always strings or symbols, when you care about insertion order separate from numeric sorting, or when you frequently add and remove entries at scale, and stay with objects when you serialize to JSON without extra steps.
Another FAQ is how iteration differs (Object.keys / Object.entries versus map.forEach) and how JSON serialization interacts with each shape—objects stringify naturally, while Map needs an explicit conversion path. If you edit the snippets in this guide, re-run your own checks so your environment’s console formatting still matches your expectations.
