Hello, I am going to give you a quick introduction about JavaScript Set() object and its must know properties and methods.
The Set() Object
The Set
object is a collection of elements. It can store primitive values or object references.Set can only contains distinctive elements,so With Set
object you can remove duplicate elements.
Using the Set object
- Setting up an empty
Set
object -
Set
object With array
Iterating Sets
- You can iterate over items in
Set
object usingfor...of
statement.
Adding an element to a set
- You can add an element to the end of a
Set
object via theadd()
method.
Number of elements in a Set
- To see the number of (unique) elements in a
Set
object use thesize
property.
Checking an element in the set
- The
has
method will tell you whether a value exists in aSet
object.
Delete elements from set
- Using
delete
method you can remove a value from theSet
object.
Remove all elements from set
- To remove all the elements from
Set
object simply useclear
method.
Hope you have learnt something from this article.Kindly forgive any mistake as it is my first article. Happy Coding!
Top comments (0)