| by Arround The Web | No comments

Set Object Methods in JavaScript

Set Objects in JavaScript are very much similar to the arrays the major difference between these two is that the set objects can’t hold the duplicate values however arrays can. In JavaScript, the set objects allow us to store only unique values in them. These unique values can be of any type like primitive data types(int, strings, etc), object references, complex object/data types such as object, arrays, literals, etc. In JavaScript several set object methods can be used to achieve different functionalities, for example, add(), delete(), forEach(), etc.

In this article, we will cover the below listed aspects of set object methods in JavaScript:

So, let’s begin!

How to use new Set() to create a set in JavaScript

To work with any of the Set object methods, firstly, we have to create a set. To do so, we can use the “new Set()” constructor.

Example

The below given piece of code will explain how to create a Set in JavaScript using the “new Set()” constructor:

<script>
let employeeNames = new Set();

console.log(employeeNames);
</script>

The above code will create an empty set as shown in the following output:

The output shows that an empty Set is created, now we can perform any functionality on that set using different set object methods such as append elements, remove elements, etc.

How to use add() method to add elements in a set

JavaScript provides a method named add() that is used to append the elements in a set.

Example

Now, we will extend the above example a little bit more to add/append the elements to the Set:

employeeNames.add("Steve");
employeeNames.add("Michael");
employeeNames.add("Smith");
employeeNames.add("Paul");
employeeNames.add("Ambrose");
console.log(employeeNames);

In this example, we added five elements in the set named “employeeNames” using the add() method. Afterward, we utilized the console.log() method to print all the elements stored in the “employeeNames” set on the browser’s console:

The output verifies the working of the add() method.

How to use delete() method to remove elements from a set

In JavaScript, the delete() method can be used to remove some specific elements from the set object.

Example

Let’s suppose we want to remove “Smith”, and “Paul” from the set “employeeNames”. To do so, we can utilize the delete() method:

employeeNames.delete("Smith");
employeeNames.delete("Paul");
console.log(employeeNames);

The above code block will generate the following output:

The above snippet shows that the delete() method has removed “Smith” and “Paul” from the Set successfully.

How to use clear() method to delete all elements from a set

In JavaScript, the clear() method can be used to remove all the elements of a set object.

Example

In this example, we will utilize the clear() method to delete all the items from the set “employeeNames”:

employeeNames.clear();
console.log(employeeNames);

The above code snippet will produce the following results:

The output shows the Set’s size equal to zero; it authenticates the working of the clear() method.

How to check the existence of some specific value in a set using has() method

In JavaScript, the has() method can be used to check whether a specific element exists in the set or not.

Example

In this example, we will check the existence of two elements i.e. “Smith”, and “Bryn” using the has() method:

console.log(employeeNames.has("Smith"));
console.log(employeeNames.has("bryn"));

The piece of code will return true if the specified value exists in the set and it will return false if the specified value doesn’t exist in the targeted set:

The output shows that the has() method returns true for the element “smith” as it exists in the set while “Bryn” doesn’t exist in the targeted set therefore the has() method returns false for it.

How to find the size of a set

In JavaScript, the size property can be used to check the size/length of some specific set.

Example

In this example we will utilize the size property to check the size of the set “employeeNames”:

Following will be the corresponding output for the above-given code:

The output shows the appropriate size of the specified set i.e “employeeNames” set.

Conclusion

In JavaScript, the set objects allow us to store only unique values in them. These unique values can be of any type like primitive data types(int, strings, etc), object references, complex object/data types such as objects, arrays, literals, etc. In JavaScript, there is a wide range of set object methods that can be used to perform different functionalities. For example, Add(), delete(), and clear(), methods are used to append and remove elements, the has() method is used to check the existence of an element in a specific set, etc. This write-up explained the working of various set object methods with the help of suitable examples.

Share Button

Source: linuxhint.com

Leave a Reply