JavaScript Set
JavaScript Set is a collection of unique element.
Creat a Set
Set() constructor can be used to create a Set in JavaScript.
</>
Copy
var set = new Set();
Add Element to Set
Map.add() method adds a new element to this Set.
</>
Copy
set.add(element);
Delete an Element from Set
Set.delete() method deletes the specified element from this Set.
</>
Copy
var isDeleted = set.delete(element);
If Set has a Specific Element
Set.has() method checks if this Set has specified element.
</>
Copy
var isElementPresent = set.has(element);
Execute a Function for Each Element
Set.forEach() method executes a specified function for each element of this Set.
</>
Copy
set.forEach (function(element) {
//code
})
JavaScript Set Tutorials
The following tutorials cover concepts on a JavaScript Set in detail with examples.
- JavaScript – Create Set
- JavaScript – Create an Empty Set
- JavaScript – Set Size/Length
- JavaScript – Add Element to Set
- JavaScript – Delete Element from Set
- JavaScript – Check if Set contains Specific Element
- JavaScript – Iterate over Elements of Set
- JavaScript – Set forEach()
- JavaScript – Convert Array into Set
Conclusion
In this JavaScript Tutorial, we learned about Sets in JavaScript.