ForgeFrontend — Prepare, Practice, Crack
Secure checkout
Lifetime access
Instant Drive delivery
Free updates forever

Prepare · Practice · Crack

When would you use Map/Set over plain objects/arrays?

Quick answer

Map for keyed collections with any key type and ordered, sized iteration; Set for unique values and fast membership.

In detail

A Map allows any key (objects included), preserves insertion order, has `.size`, and is optimised for frequent adds/deletes — better than an object for a true dictionary. A Set stores unique values with O(1) `has`, ideal for de-duplication and membership checks.

const m = new Map();
m.set(objKey, 1); m.get(objKey); m.size;
const s = new Set([1, 2, 2]); s.has(2); // true, size 2

Why interviewers ask this: Tests data-structure judgement beyond objects/arrays.

This is 1 of 118+ questions in the JavaScript Interview Kit

Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.

⚡ Get the JavaScript Interview Kit → ₹299

Full kit

JavaScript Interview Kit · ₹299

Get it →