JavaScript Set

JavaScript Set is a collection of unique element.

Creat a Set

Set() constructor can be used to create a Set in JavaScript.

var set = new Set();
ADVERTISEMENT

Add Element to Set

Map.add() method adds a new element to this Set.

set.add(element);

Delete an Element from Set

Set.delete() method deletes the specified element from this Set.

var isDeleted = set.delete(element);

If Set has a Specific Element

Set.has() method checks if this Set has specified element.

var isElementPresent = set.has(element);

Execute a Function for Each Element

Set.forEach() method executes a specified function for each element of this Set.

set.forEach (function(element) {
    //code
})

JavaScript Set Tutorials

The following tutorials cover concepts on a JavaScript Set in detail with examples.

Conclusion

In this JavaScript Tutorial, we learned about Sets in JavaScript.