
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When to Use Sets in JavaScript
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
Advertisements