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
Setobject
-
Setobject With array
Iterating Sets
- You can iterate over items in
Setobject usingfor...ofstatement.
Adding an element to a set
- You can add an element to the end of a
Setobject via theadd()method.
Number of elements in a Set
- To see the number of (unique) elements in a
Setobject use thesizeproperty.
Checking an element in the set
- The
hasmethod will tell you whether a value exists in aSetobject.
Delete elements from set
- Using
deletemethod you can remove a value from theSetobject.
Remove all elements from set
- To remove all the elements from
Setobject simply useclearmethod.
Hope you have learnt something from this article.Kindly forgive any mistake as it is my first article. Happy Coding!
Top comments (0)