Q28: What is the difference between Map, Set, WeakMap, and WeakSet?

  • Map: Key-value store accepting any data type key.
  • Set: Collection of unique values.
  • WeakMap / WeakSet: Hold garbage-collectable weak references strictly to objects/symbols. Their keys/elements are non-enumerable.
Key Takeaways
  • Map supports keys of any type; WeakMap keys MUST be objects/symbols.
  • WeakMap references do not prevent garbage collection if no other references exist.
Knowledge Check

Why are WeakMap keys non-enumerable?