Properties of Set in JavaScript
Set.size – It returns the number of elements in the Set.
Sets in JavaScript
Sets in JavaScript are collections of unique values, meaning no duplicates are allowed. They provide efficient ways to store and manage distinct elements. Sets support operations like adding, deleting, and checking the presence of items, enhancing performance for tasks requiring uniqueness.
Syntax:
new Set([it]);
Parameter:
- it: It is an iterable object whose all elements are added to the new set created, If the parameter is not specified or null is passed then a new set created is empty.
Return Value:
A new set object.
Example: This example shows the implementation of a JavaScript set.
// ["sumit","amit","anil","anish"]
let set1 = new Set(["sumit","sumit","amit","anil","anish"]);
// it contains 'f', 'o', 'd'
let set2 = new Set("fooooooood");
// it contains [10, 20, 30, 40]
let set3 = new Set([10, 20, 30, 30, 40, 40]);
// it is an empty set
let set4 = new Set();
Table of Content
- Properties of Set in JavaScript:
- Methods of Set in JavaScript:
- 1. Set.add()
- 2. Set.delete()
- 3. Set.clear()
- 4. Set.entries()
- 5. Set.has()
- 6. Set.values()
- 7. Set.keys()
- 8. Set.forEach()
- 9. Set.prototype[@@iterator]()
- Set Operations in JavaScript
- JavaScript subSet() Method:
- JavaScript union() Method:
- JavaScript intersection() Method:
- JavaScript difference() Method: