Full answer
A Set is a collection of unique values. Adding a value that is already present does nothing. It is the quick way to remove duplicates or to test membership with has. Equality uses the SameValueZero rule, so NaN equals NaN and 0 equals -0, but two different objects are never equal.
A Map stores pairs of key and value. Unlike an object, whose keys become strings or symbols, a Map key can be anything: a number, an object, even a function. Maps remember insertion order and expose size. They have get, set, has, delete, and clear, and they are directly iterable with for...of, yielding [key, value] pairs.
Compared with a plain object used as a dictionary, a Map has no prototype keys (so a key such as constructor is not a problem), handles frequent additions and removals well, and keeps keys of any type distinct. Objects are better for fixed records and for data that must be serialized with JSON.stringify, because Maps and Sets do not serialize by default.
WeakMap and WeakSet hold their keys weakly, so an object used as a key can still be garbage collected. They are not iterable and have no size.
const attendance = ['Asha', 'Ravi', 'Asha', 'Meera', 'Ravi'];
const unique = new Set(attendance);
console.log(unique.size, [...unique], unique.has('Ravi'));
const nanSet = new Set([NaN, NaN, 0, -0]);
console.log(nanSet.size);
const roomByStudent = new Map();
const key = { id: 1 };
roomByStudent.set('Asha', 'Room 4');
roomByStudent.set(key, 'Room 9');
roomByStudent.set(1, 'number key').set('1', 'string key');
console.log(roomByStudent.get(key), roomByStudent.get(1), roomByStudent.get('1'));
console.log(roomByStudent.size);
for (const [studentKey, room] of roomByStudent) {
if (typeof studentKey === 'string' && studentKey !== '1') console.log(studentKey, room);
}
const plain = {};
plain[key] = 'a';
plain[{ id: 2 }] = 'b'; // both keys turn into "[object Object]"
console.log(Object.keys(plain));
console.log('constructor' in {}, new Map().has('constructor'));
console.log(JSON.stringify({ set: unique, map: roomByStudent }));
console.log(JSON.stringify([...unique]), JSON.stringify(Object.fromEntries([['a', 1]])));Run with Node.js v24.22.1 as js-22.js; two runs gave identical output. Output: 3 [ 'Asha', 'Ravi', 'Meera' ] true 2 Room 9 number key string key 4 Asha Room 4 [ '[object Object]' ] true false {"set":{},"map":{}} ["Asha","Ravi","Meera"] {"a":1}
Key points
- Set: unique values; Map: key-value pairs with keys of any type.
- Both preserve insertion order and have size.
- Equality uses SameValueZero; objects are compared by reference.
- Map avoids inherited keys and string-only keys of plain objects.
- Maps and Sets are not serialized by JSON.stringify without conversion.
Common mistakes
- Using an object as a map with object keys; all keys collapse into the same string.
- Expecting two equal-looking objects to be one entry in a Set.
- Calling JSON.stringify on a Map or Set and getting an empty object.
- Using a plain object for a dictionary of user-supplied keys and being hit by keys like __proto__ or constructor.
Follow-ups
- What is the difference between Map and WeakMap, and when do you want weak keys?
- How would you find the intersection of two arrays using Set?
- What is the time complexity of Set.has compared with Array.includes?
Tips
- Start with the one-line purpose of each, then give the three object-versus-Map differences.
- Show the Set deduplication one-liner; it is the answer to many coding warm-ups.
- State the serialization limitation; it is a practical trade-off.