When should you use sets in Javascript?

Web DevelopmentFront End TechnologyJavascript

Whenever you want to store unique elements in a container for which the order doesn't matter and you mainly want to use it to check for membership of different objects.

Sets are also useful when you want to perform operations like union, intersection, a difference like you do in mathematical sets.

Let's look at both how we can define our own set and use the existing one in ES6.

Methods we'll implement

The ES6 set API provides some methods. We'll implement these methods in our implementation and also look at how to use them using the built-in class.

  • add() −  Adds a new element to the set
  • clear() − Removes all elements from the set
  • delete() − Deletes a certain element from the set
  • has( − Checks if a value exists in the set or not
  • values()  − Returns all the values in the set
raja
Published on 27-Jun-2018 11:57:47
Advertisements