- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 should you 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
- Related Articles
- When you should not use JavaScript Arrow Functions?
- When should you use 'friend' in C++?
- When should you use a class vs a struct in C++?
- When should I use an Inline script and when to use external JavaScript file?
- When should I use a semicolon after curly braces in JavaScript?
- When Should I use Selenium Grid?
- When should I use MySQL compressed protocol?
- When should I use a composite index in MySQL?
- Why should we use element in JavaScript?
- Why You Should Use an Anonymous Proxy Server?
- Explain sets in JavaScript?
- Why should we not use ++, -- operators in JavaScript?
- When to use new operator in C++ and when it should not be used?
- When should I use the keyword ‘this’ in a Java class?
- Adding two Sets in Javascript

Advertisements