JavaScript Set Object


The JavaScript Set object is used to store the elements with unique values. The values can be of any type i.e. whether primitive values or object references.

Parameter

iterable - It represents an iterable object whose elements will be added to the new Set.

Points to remember

  • A set object uses the concept of keys internally.
  • A set object cannot contain the duplicate values.
  • A set object iterates its elements in insertion order.

A Set is a special type collection – “set of values” (without keys), where each value may occur only once.

Its main methods are:

  • 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.

The main feature is that repeated calls of set.add(value) with the same value don’t do anything. That’s the reason why each value appears in a Set only once.

For example, we have visitors coming, and we’d like to remember everyone. But repeated visits should not lead to duplicates. A visitor must be “counted” only once.