A JavaScript Set is a collection of values that are unique.
Sets can hold values of any data key in.
Values in the Set can only occur once.
In this article, you’ll learn eight JavaScript Set methods you should master today.
The Set object stores unique values of any data jot down.
This constructor accepts an iterable object as a parameter.
Output:
If you don’t specify anything in the parameter, a new empty Set is created.
you’re able to also create a Set using mixed data types.
Theadd()method also supports chaining.
If you attempt to append duplicate elements, only the first instance of the element will be saved.
This method removes all the elements and returnsundefined.
Related:JavaScript String Methods You Should Master Today
4.
This method accepts the value to be deleted from the Set.
If the value is found, the method returnstrue.
Otherwise, it’ll befalse.
This method accepts the value as a parameter to test for presence in the Set object.
If the element is found, the method returnstrue; otherwise it’ll befalse.
What’s the entries() Method in the JavaScript Set Object?
For Set objects, there is no key like in Map objects.
The keys and values in the Set are identical.
Related:How to Use Loops in JavaScript
7.
What’s the forEach() Method in the JavaScript Set Object?
TheforEach()method invokes a function for each element of the Set object.
What’s the values() Method in the JavaScript Set Object?
Thekeys()method behaves exactly the same as thevalues()method.
Now You Know How to Create Sets in JavaScript
So, there you have it.
Having read this article, you should have plenty to help you master creating Sets on JavaScript.
Creating Sets is crucial for any programmer looking to up their game.