🧵 JavaScript Set object 🧵
It stores the collection of unique values of any type in JavaScript.
→ Set Methods
1) Create a new Set from the Array
let uniqueAlpha = new Set(alphabets);
console.log(uniqueAlpha); // Set { 'a', 'b', 'c', 'd' , 'e', 'f'}
2) Size of the Set
let uniqueAlphaSize = uniqueAlpha.size
console.log(uniqueAlphaSize) // 6
3) Add the element to a Set
uniqueAlpha.add('z');
console.log(uniqueAlpha); // Set { 'a', 'b', 'c', 'd' , 'e', 'f', 'z'}
4) Check if the value is in the Set
let hasA = uniqueAlpha.has('a')
let hasK = uniqueAlpha.has('k')
console.log(hasA) // true
console.log(hasK) // false
5) Remove elemnets from a Set
uniqueAlpha.delete('f');
console.log(uniqueAlpha); // Set {"a", "b", "c", "d", "e","z"}
6) Remove all elements from a Set
uniqueAlpha.clear()
console.log(uniqueAlpha) // Set {} => uniqueAlpha.size = 0
→ Weak Set
It is similar to the set but it contains only objects. It gets automatically garbage collected therefore it does not have size method.