Sets in JavaScript

Sets in JavaScript

It is a built-in object that allows us to store unique values of any type. This means a value in the set may only occur once, it is unique in the set's collection. The values in a Set can be iterated in insertion order, and you can check whether a value is in the Set or remove a value from the Set. The insertion order corresponds to the order in which each element was inserted into the set by the add()

There are many instance methods available in sets. Some of them are given below:

  1. Set.prototype.add() - the add() method is a built-in function of the Set object. It is used to add a new element to a Set. The syntax for using the add() method is as follows: myNewSet.add(element);
    Here, "myNewSet" is the Set object to which the element will be added, and "element" is the value to be added to the Set. The add() method returns the Set object itself, so you can chain multiple add() methods together if you want to add multiple elements to the Set at once.

    Ex- const myNewSet = new Set();

    myNewSet.add(1);

    myNewSet.add(2);

    myNewSet.add(3);

    This would create a new Set object called "myNewSet" and add the numbers 1, 2, and 3 to it. The Set would contain unique values, so if you tried to add a value that was already in the Set, it would be ignored.

  2. Set.prototype.clear() - The clear() method is a built-in function of the Set object. It is used to remove all elements from a Set. The syntax is given below:

    myNewSet.clear();

    The clear() method does not take any arguments and returns undefined.

    For example, let's say you have a Set of fruits that you want to clear.

    const fruits = new Set(['apple', 'mango', 'banana']);

    console.log(fruits); // Set { 'apple', 'mango', 'banana' }
    fruits.clear();
    console.log(fruits); // Set {}
    This would create a new Set object called "fruits" and add the values 'apple', 'mango', and 'banana' to it. The clear() method is then called to remove all elements from the Set. The first console.log() statement would output the Set with its original values, and the second console.log() statement would output an empty Set, since all elements have been cleared.

  3. Set.prototype.delete()The delete() method is a built-in function of the Set object. It is used to remove a specific element from a Set. The syntax for using the delete() method is as follows:
    myNewSet.delete(element);
    Here, "myNewSet" is the Set object from which the element will be removed, and "element" is the specific value to be removed from the Set. The delete() method returns a boolean value indicating whether the element was successfully removed from the Set (true) or not (false).
    For example, let's say you have a Set of subjects and you want to remove a specific subject from the Set. You could use the following code to remove the subject:
    const subejcts = new Set(['english', 'maths', 'science', 'computer']); console.log(subjects); // Set { 'english', 'maths', 'science', 'computer' } subjects.delete('maths');
    console.log(subjects); // Set { 'english', 'science', 'computer' }
    The first console.log() statement would output the Set with its original values, and the second console.log() statement would output the Set with the 'maths' element removed.

  4. Set.prototype.has(): The has() method is a built-in function of the Set object. It is used to check if a specific element exists in a Set. The syntax for using the has() method is as follows:

    myNewSet.has(element);
    The has() method returns a boolean value indicating whether the element exists in the Set (true) or not (false).
    For example, let's say you have a Set of fruits and you want to check if the Set contains the fruit "apple". You could use the following code to check for the presence of "apple" in the Set:
    const fruits = new Set(['banana', 'orange', 'apple', 'kiwi']); console.log(fruits.has('apple')); // true
    console.log(fruits.has('grape')); // false
    The first console.log() statement would output true, since 'apple' is present in the Set, and the second console.log() statement would output false, since 'grape' is not present in the Set.

    In conclusion, JavaScript Set is a powerful built-in data structure that provides developers with a simple and efficient way to handle collections of unique values. With its various methods such as add(), delete(), clear(), has(), and many more, Sets allow for quick and easy manipulation of data. Whether you need to remove duplicate values from an array, or simply manage a collection of unique data, Sets can be a valuable tool in your JavaScript programming arsenal.