HOWTO · JavaScript
How to Create Associative Arrays in JavaScript
This tutorial shows you how you can build associative arrays in JavaScript using objects and Map().
On this page
“Associative array” is a useful search phrase for key–value storage, but JavaScript arrays are indexed collections: their elements are accessed by numeric positions. For separate keys and values, use a Map or an object. A Map is designed to hold entries with keys of any value type; an object is useful for properties that describe a record or other property-shaped data.
The distinction matters when reading or writing with brackets. map[key] accesses an ordinary object property on the Map instance, while object[key] accesses a property in the object. The diagram summarizes the two models; the examples below remain selectable so you can adapt the operations.
Map entries and object properties use different access operations.
How JavaScript Represents Key–Value Data
JavaScript arrays are intended for ordered, index-based elements such as a list of names. They are not associative arrays in the sense used by languages with dictionary-style subscripting. For example, assigning a named property to an array does not turn that property into an array element or make it part of the array’s normal iteration.
For key–value data, choose between the built-in Map type and an ordinary object. A Map stores entries separately from its other properties, remembers entry insertion order, and accepts keys of any type, including objects and functions. An object stores properties whose keys are strings or symbols; it is commonly used to represent a record with named fields.
Use Map for Key–Value Entries
Create a Map with new Map(). Use set(key, value) to add or replace an entry, get(key) to retrieve its value, has(key) to test for an entry, and delete(key) to remove one. A Map is iterable, so a for...of loop visits its entries in insertion order. The following self-contained example uses strings as keys and demonstrates these operations:
Run this example:
const scores = new Map();
scores.set('Mira', 18);
scores.set('Noah', 21);
const miraScore = scores.get('Mira');
const hasNoah = scores.has('Noah');
scores.delete('Noah');
for (const [name, score] of scores) {
console.log(`${name}: ${score}`);
}
console.log(`Mira's score: ${miraScore}`);
console.log(`Noah was present: ${hasNoah}`);
console.log(`Noah remains: ${scores.has('Noah')}`);
Mira: 18
Mira's score: 18
Noah was present: true
Noah remains: false
The iterator yields each key and value as a pair, which makes destructuring in the loop convenient. If the same key is set again, its value is updated rather than creating a second entry. Map key matching uses SameValueZero; for object keys, that means the same object reference must be used to find the entry.
Why Bracket Properties Do Not Add Entries to a Map
Bracket assignment on a Map adds an ordinary property rather than a Map entry. For example, scores['Mira'] = 18 creates a property on the Map object, but it does not affect scores.size, Map iteration, or scores.has('Mira'). The property can be read with scores['Mira'], which can make this mistake look successful, but Map methods only operate on entries created through Map operations such as set().
Use scores.set('Mira', 18) for an entry, then scores.get('Mira') to read it. This distinction also applies when the bracket key is a number or another value: JavaScript property names are strings or symbols, while Map keys retain their value types. Keep one access model consistent throughout the code so the data can be found, iterated, and removed as intended.
Use an Object for String-Keyed Properties
An object is a natural fit when the values form a record with named properties. Dot notation works when the property name is a valid identifier, as in person.firstName. Bracket notation evaluates an expression and works for property names that contain spaces or come from a variable:
const person = {};
person.firstName = 'Mira';
person['preferred language'] = 'JavaScript';
const propertyName = 'firstName';
console.log(person[propertyName]);
console.log(person['preferred language']);
Mira
JavaScript
In object literals, quote a key that is not a valid identifier, for example { 'preferred language': 'JavaScript' }. An object’s property access does not provide Map’s entry operations: use Object.keys(), Object.entries(), or a for...in loop when you need to enumerate properties. A for...in loop can also visit inherited enumerable properties, so use an own-property check if that distinction matters.
For objects used as dictionaries with arbitrary user-provided keys, account for inherited properties and special names. A Map is often simpler for that job because its entries are separate from the object’s built-in properties. Objects are especially clear when the keys are a known set of fields that describe one value.
Choose Between Map and an Object
Choose Map when you need a dedicated collection of key–value entries, keys that are not just strings or symbols, reliable entry iteration, or the size, has(), and delete() operations. Choose an object when the data has named fields, needs to be represented as a typical JavaScript record, or is naturally consumed through property access. Both can store values, but their key rules and iteration APIs differ.
There is no need to rely on a blanket claim about browser support. If a project targets older or constrained JavaScript environments, check the actual runtimes and required language features for that project. For general code, start with the data shape: use a Map for a collection of entries and an object for a value whose fields have names.