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

JS Puzzles · Const

Map vs Object

Map is a keyed collection like Object, but it accepts keys of any type, including objects, and provides convenient methods plus size. Use it for dynamic keyed data; use Object when the shape and string-like keys are known in advance.

How it works

Use Map for a keyed collection when keys are not known in advance or may be objects and other non-string values. Use a plain object when the data shape is known and its property keys are strings or symbols.

Syntax

new Map([iterable])

Parameters

NameTypeDescription
iterableIterable<readonly [any, any]>The iterable of key-value pairs used to initialize the Map; omitted when creating an empty Map.

Returns

A Map object containing the supplied key-value pairs.

Key points

  • Map supports keys of any type, including objects, functions, and primitives; Object property keys are limited to strings and symbols.
  • Map iteration follows insertion order, which makes traversal order explicit for keyed data.
  • Map adds collection-oriented operations such as set(), get(), has(), delete(), clear(), and the size property.
  • Map reports its number of entries through size, avoiding manual counting required for an Object.
  • Object.entries() produces key-value pairs that can be passed to Map to create a Map from a plain object.

Common mistakes

  • An object key in a plain Object is converted to a string, so different object references can collide instead of remaining distinct keys.
  • Map preserves insertion order, unlike a regular Object; do not assume both collections have identical iteration behavior.
  • A missing Map key yields undefined, which is also a possible stored value; use has() when presence must be distinguished.

Example: Using an object key with Map

const objectKey = { id: 1 };
const map = new Map([[objectKey, "value"]]);

console.log(map.get(objectKey)); // "value"
console.log(map.size); // 1
console.log(Object.keys({ a: 1 })); // ["a"]

Map preserves an object key as an object and exposes size directly; Object.keys returns an array for an object.

When to use it

Dynamic dictionaries

Keys arrive dynamically or come from external input.

Reference-keyed lookup

Objects, functions, or other non-string values identify entries.

Ordered keyed data

The collection needs direct size access and insertion-order iteration.

Interview questions

QWhat is the main difference between Map and Object?

Map is a keyed collection like Object, but its keys may have any type, including objects. A plain Object uses string or symbol property keys, while Map also provides convenient methods and size.

Follow-up: When should an Object be preferred? Prefer it when the shape is known ahead of time and all keys can be represented as strings.

QWhy can Map use objects as keys?

Map keys are not converted to strings, so object references can be used directly as keys. The same object reference must be supplied to retrieve the associated value.

Follow-up: What happens with an object key in Object? It is converted to a string property key, so separate object keys do not remain distinct.

QWhat conveniences does Map provide over a plain Object?

Map has methods such as set(), get(), and has(), plus the size property. Its entries iterate in insertion order, whereas Object uses Object.keys(), Object.values(), or Object.entries() for comparable operations.

Follow-up: How can a plain object be converted to a Map? Pass Object.entries(obj) to the Map constructor.

  • Map
  • Object
  • Object.entries()
  • map.set(key, value)
  • map.get(key)
  • map.has(key)
  • map.delete(key)
  • map.clear()

Source: Map and Set. Checked 2026-10-15.