jsguides

Reference

Map and Set

Map, Set, WeakMap, and WeakSet collection types.

  1. Map

    The Map object stores key-value pairs with any type of key while preserving insertion order. Learn methods, patterns, and when to use Map over plain objects.

  2. Map.groupBy()

    Map.groupBy() groups the elements of an iterable into a Map, using the values returned by a callback as keys and the matched elements as values.

  3. Map.prototype.clear()

    Map.prototype.clear() removes all key-value pairs from a Map in one call, resetting size to 0 — ideal for emptying caches, stores, and listener maps.

  4. Map.prototype.delete()

    Map.prototype.delete() removes an entry by key from a JavaScript Map, returning true when the key existed. Use it for cache invalidation and targeted cleanup.

  5. Map.prototype.entries()

    Map.prototype.entries() returns an iterator yielding [key, value] pairs in insertion order. Use it to access both keys and values at once during Map iteration.

  6. Map.prototype.forEach()

    Map.prototype.forEach() executes a callback once for each key-value pair in insertion order, supporting thisArg binding and mutation during iteration.

  7. Map.prototype.get()

    Map.prototype.get() retrieves values by key from a Map, returning undefined for missing entries. Pair it with has() to tell missing keys from stored undefineds.

  8. Map.prototype.has()

    Map.prototype.has() checks whether a key exists in a Map, returning a boolean. Uses SameValueZero so NaN matches itself and object keys compare by reference.

  9. Map.prototype.keys()

    Map.prototype.keys() returns a new iterator yielding each key from a Map in insertion order, giving direct access to the key set without the associated values.

  10. Map.prototype.set()

    Map.prototype.set() adds or updates a key-value pair in a JavaScript Map, returning the Map itself so you can chain multiple calls in one expression.

  11. Map.prototype.values()

    Map.prototype.values() returns an iterator of all values in a JavaScript Map in insertion order, giving you access to stored data without the keys.

  12. Set

    JavaScript Set stores unique values with O(1) lookup, ideal for deduplication, membership testing, and set operations like union and intersection.

  13. Set.prototype.add()

    The Set.prototype.add() method inserts values into a JavaScript Set with strict equality — covering primitives, objects, chaining, and deduplication patterns.

  14. Set.prototype.clear()

    Set.prototype.clear() removes all elements from a Set. Learn when to use clear() versus creating a new collection, plus memory and reference trade-offs.

  15. Set.prototype.delete()

    Set.prototype.delete() removes a value from a JavaScript Set and returns true if it was found. Use it for deduplication and conditional cleanup.

  16. Set.prototype.difference()

    Set.prototype.difference() returns a new Set with the elements that exist in the receiver but are absent from the argument. Accepts any set-like object.

  17. Set.prototype.entries()

    Set.prototype.entries() yields [value, value] pairs from a Set in insertion order, mirroring Map so generic iteration code works on both collections.

  18. Set.prototype.forEach()

    Set.prototype.forEach() calls a callback once per value in a Set, in insertion order. Returns undefined — use for side effects like logging or updating state.

  19. Set.prototype.has()

    Use Set.prototype.has() to test whether a JavaScript Set contains a specific value. Returns a boolean with O(1) lookup performance even for large collections.

  20. Set.prototype.intersection()

    Set.prototype.intersection() returns a new Set of elements present in both this set and another set-like object. ES2025 method with SameValueZero equality.

  21. Set.prototype.isDisjointFrom()

    Set.prototype.isDisjointFrom() returns true when a Set shares no elements with another Set or set-like object. Added in ES2025 to modern browsers.

  22. Set.prototype.isSubsetOf()

    Set.prototype.isSubsetOf() returns true when every element of a Set is also in another Set or set-like object. ES2025, uses SameValueZero.

  23. Set.prototype.isSupersetOf()

    Set.prototype.isSupersetOf() returns true when every element of another Set or set-like object is also in the receiver. ES2025, SameValueZero.

  24. Set.prototype.keys()

    Set.prototype.keys() returns a new Set Iterator yielding each value in insertion order. Functionally identical to values(), it exists for API symmetry with Map.

  25. Set.prototype.size

    Set.prototype.size returns the number of unique elements in a JavaScript Set as a read-only getter property with O(1) access time.

  26. Set.prototype.symmetricDifference()

    Set.prototype.symmetricDifference() returns a new Set with elements in exactly one of two sets. Non-mutating, accepts set-like objects.

  27. Set.prototype.union()

    Use Set.prototype.union() to return a new Set containing every element present in either of two sets. ES2025, non-mutating, accepts set-like objects.

  28. Set.prototype.values()

    Set.prototype.values() returns a new Iterator yielding each value in a JavaScript Set in insertion order, enabling sequential access and array conversion.

  29. WeakMap

    WeakMap is a collection of key-value pairs where keys are objects and values can be any type. Unlike Map, it does not prevent garbage collection of its keys.

  30. WeakSet

    WeakSet is a JavaScript collection that stores object references weakly, allowing automatic garbage collection when objects are no longer referenced elsewhere.