Set and Weakset

Questions
  1. How set is different from Map
  2. How set is different from Array
  3. Set vs WeakSet

Set


A set is a special type of collection in Javascript Unless an array set holds only unique values without any keys where each value occurs only once.

Salient Features of Set

Set methods

  • new Set(iterable) โ€“ creates the set, and if an iterable object is provided (usually an array), copies values from it into the set.
  • set.add(value) โ€“ adds a value, returns the set itself.
  • set.delete(value) โ€“ removes the value, returns true if value existed at the moment of the call, otherwise false.
  • set.has(value) โ€“ returns true if the value exists in the set, otherwise false.
  • set.clear() โ€“ removes everything from the set.
  • set.size โ€“ is the elements count.

Set Iteration Methods

  • set.keys() โ€“ returns an iterable object for values,
  • set.values() โ€“ same as set.keys(), for compatibility with Map,
  • set.entries() โ€“ returns an iterable object for entries [value, value], exists for compatibility with Map.

WeakSet


WeakSet behave similarly as a Set but it only allows objects as values, primitive values are not allowed. But WeakSet do not support iteration methods/properties like weakSet.keys() or Size

And WeakSet also has the nature of WeakMap which is automatic garbage collection if any value in the WeakSet don't have any references

Last updated on by Abhin Pai