JavaScript Set Methods

JavaScript Set Methods
JavaScript Set Methods come with built-in methods to add, delete, check, loop, and manage unique values.
List of Common Set Methods
| Method | Description |
|---|---|
add() | Adds a new value |
delete() | Removes a specific value |
has() | Checks if a value exists |
clear() | Removes all values |
size | Returns number of elements (property, not a function) |
forEach() | Iterates through set values |
values() | Returns an iterator of values |
keys() | Same as values() (for compatibility with Maps) |
entries() | Returns [value, value] pairs (like Map) |
1. add()
2. delete()
3. has()
4. clear()
5. size (Property)
6. forEach()
7. values() Method
8. keys() Method (Same as values())
9. entries() Method
Useful when using Set where Map-like structure is needed.
Example Using Multiple Methods
Output:
When to Use Sets?
- Removing duplicates
- Fast value lookup
- Unique collections (tags, IDs, categories)
