JavaScript Set Methods

JavaScript Set Methods

JavaScript Set objects 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:

true
3
Apple
Banana
Orange

🧠 When to Use Sets?

✔ Removing duplicates
✔ Fast value lookup
✔ Unique collections (tags, IDs, categories)

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *