In JavaScript, a Set is a built-in object that represents a collection of unique values, where each value can occur only once. Sets are similar to Arrays, but with no duplicate values. They can store any data type, including primitive types like strings and numbers, as well as objects and other Sets.
You can create a Set by using the new Set()
constructor and adding elements with the .add()
method. You can also check the size of a Set with the .size
property and remove elements with the .delete()
method. Sets also have useful methods like .has()
to check if a value exists in the Set and .clear()
to remove all elements.
Overall, Sets provide a convenient and efficient way to work with unique collections of values in JavaScript.
new Set()
This method creates a new Set object.
Syntax: const mySet = new Set();
Example:
const mySet = new Set();
mySet.add("apple");
mySet.add("banana");
mySet.add("orange");
console.log(mySet); // Set(3) {"apple", "banana", "orange"}
Output: Set(3) {"apple", "banana", "orange"}
add()
This method adds a new element to the Set.
Syntax: mySet.add(value);
Example:
const mySet = new Set();
mySet.add("apple");
mySet.add("banana");
mySet.add("orange");
mySet.add("apple");
console.log(mySet); // Set(3) {"apple", "banana", "orange"}
Output: Set(3) {"apple", "banana", "orange"}
delete()
This method removes an element from the Set.
Syntax: mySet.delete(value);
Example:
const mySet = new Set();
mySet.add("apple");
mySet.add("banana");
mySet.add("orange");
mySet.delete("banana");
console.log(mySet); // Set(2) {"apple", "orange"}
Output: Set(2) {"apple", "orange"}
has()
This method returns true if a value exists in the Set.
Syntax: mySet.has(value);
Example:
const mySet = new Set();
mySet.add("apple");
mySet.add("banana");
mySet.add("orange");
console.log(mySet.has("apple")); // true
console.log(mySet.has("grape")); // false
Output: true
and false
forEach()
This method invokes a callback for each element in the Set.
Syntax: mySet.forEach(callback);
Example:
const mySet = new Set();
mySet.add("apple");
mySet.add("banana");
mySet.add("orange");
mySet.forEach((value) => {
console.log(value);
});
Output: "apple"
, "banana"
, "orange"
values()
This method returns an iterator with all the values in a Set.
Syntax: mySet.values();
Example:
const mySet = new Set();
mySet.add("apple");
mySet.add("banana");
mySet.add("orange");
const iterator = mySet.values();
console.log(iterator.next().value); // "apple"
console.log(iterator.next().value); // "banana"
console.log(iterator.next().value); // "orange"
Output: "apple"
, "banana"
, "orange"
size
This property returns the number of elements in a Set.
Syntax: mySet.size
Example:
const mySet = new Set();
mySet.add("apple");
mySet.add("banana");
mySet.add("orange");
console.log(mySet.size); // 3
Output: 3
Summarizing Up
JavaScript Sets are built-in objects that allow you to store unique values of any type, including objects and other Sets. With Set methods like add()
, delete()
, has()
, forEach()
, and values()
, you can add, remove, check for existence, loop over, and retrieve all the unique values in a Set. The size
property returns the number of elements in a Set. Sets provide a convenient way to work with unique collections of data in JavaScript, helping to avoid duplicates and simplifying operations like filtering or merging data. Overall, Sets are a useful and efficient data structure in JavaScript.