Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Class

Map

Map stores unique key-value pairs and preserves key insertion order. Any JavaScript value can be used as a key or a value.

How it works

A Map stores unique key-value pairs and preserves the keys' insertion order during iteration. Keys and values can be any JavaScript values; use methods such as set(), get(), has(), and delete() to modify or query entries.

Syntax

new Map()
new Map(iterable)

Parameters

NameTypeDescription
iterableIterableAn optional iterable of key-value pairs used to initialize the new Map.

Returns

A new Map object.

Key points

  • Map preserves the order in which keys were first inserted, which makes ordered traversal predictable.
  • Unlike object property keys, Map keys may be objects, functions, symbols, primitives, or other values.
  • A Map has at most one entry for each key.
  • for...of receives each entry as a two-element array containing its key and value.
  • Read map.size to obtain the current number of entries.
  • Calling new Map() returns an empty Map; passing an iterable populates it during construction.

Common mistakes

  • Setting an existing key replaces its value rather than creating a second entry.
  • Two separate objects with identical contents are different keys because object-key identity is reference-based.
  • Map iteration yields two-element [key, value] arrays, so destructuring is useful in for...of loops.

Example: Create and iterate a Map

const cache = new Map([
  ["language", "JavaScript"],
  ["version", 2024],
]);

cache.set("runtime", "Node.js");
console.log(cache.get("language"));
console.log(cache.has("version"));
console.log(cache.size);

for (const [key, value] of cache) {
  console.log(`${key}: ${value}`);
}

Creates a Map from iterable key-value pairs, adds an entry, reads values, checks membership, reports size, and iterates in insertion order.

When to use it

Keyed lookup

Associate metadata or computed results with object, function, or primitive keys without converting keys to strings.

Ordered entries

Maintain an insertion-ordered collection of entries for traversal or serialization.

Caches and registries

Track membership and retrieve the number of stored entries with dedicated collection operations.

Interview questions

QWhat distinguishes Map from a plain object?

Map stores key-value pairs, allows any value as a key, and preserves key insertion order. Each key is unique, so assigning an existing key updates its associated value.

Follow-up: How do object keys compare? They compare by reference, so separately created but structurally identical objects do not refer to the same Map entry.

QHow does the Map constructor initialize entries?

The constructor accepts an iterable whose elements are key-value pairs and creates a new Map. With no argument, it creates an empty Map.

Follow-up: How would you initialize a Map with existing data? Pass an iterable such as an array of two-element arrays to new Map().

QHow do you inspect and iterate a Map?

Map is directly iterable, and iteration produces key-value pairs in insertion order. The size property gives the number of entries without manually counting them.

Follow-up: Which common loop form consumes those pairs? Use for (const [key, value] of map) to destructure each two-element pair.

  • Map.prototype.set()
  • Map.prototype.get()
  • Map.prototype.has()
  • Map.prototype.delete()
  • Map.prototype.clear()
  • Map.prototype.entries()
  • Map.prototype.keys()
  • Map.prototype.values()

Source: Map. Checked 2026-10-15.