JavaScript

Object Set Methods in JavaScript

In JavaScript, the “Set” object permits you to store any type of unique values in it, whether they are object references, primitive values such as strings or integers, or complex data types such as Object literals and arrays. A value present in a Set Object can only occur once and is not repeated.

Different Object Set methods are offered by JavaScript such as add(), delete(), clear(), and has(). The “add()” object Set method is used for appending values to the set object, delete() and clear() object set methods for deleting a specific or all elements at once, and lastly, the “has()” method is utilized for searching any element in the created Set.

This write-up will discuss the object Set methods in JavaScript. Moreover, we will also demonstrate examples related to each object Set methods such as add(), delete(), clear(), and has(). So, let’s start!

Creating a new Set Object in JavaScript

Before moving towards the object Set methods, the first thing you need to do is to create a new Set object. Using the new Set() constructor, you can easily create a new Set object in your JavaScript program. After creating the Set object, add any data type values you want to store.

In the below-given example, we will create a new “setObj1” Set object and then add some numeric values in it such as “2”, “5”, “19”, and “98”:

const setObj1 = new Set([2, 5, 19, 98]);
console.log(setObj1);

After executing the provided code, the console window will show you the type of the created object, which is “Set,” and then the values we have passed to the constructor:

You can also create an empty Set object in the following way:

const setObj2 = new Set();
console.log(setObj2);

The output will declare that “setObj2” is an empty Set object with size “0”:


As we have already mentioned, Set comprises unique values. So, when adding repeated values or elements, remember that the Set object will remove the duplicated entries and return the unique set of values.

We will create a new Set object: “setObj3” and specify repeated elements values in the constructor. After that, we will call the “console.log()” function to check the elements stored in the setObj3:

const setObj3 = new Set([4, 7, 9, 2, 1, 4, 7, 1]);
console.log(setObj3);

You can see from the output that setObj3 comprises only unique values:


At this point, you must be wondering that what if I want to create a Set that will have mixed data types? Yes, you can also store elements having different data types in your Set object.

For instance, we want to add a string, some numeric values, and an array as our Set elements. So, we will pass the desired values such as “linuxhint” string with the quotation marks, then “2” and “4” numeric values, and lastly an array “[66,99,100]” enclosed in “[ ]” brackets, all together in the Set constructor:

const setObj4 = new Set(["linuxhint", 2, 4, [66, 99, 100]]);
console.log(setObj4);

After executing the above-given code, a mixed date type Set Object “setObj4” will be created successfully:

Appending elements to a Set Object in JavaScript

The “add()” method is used for appending a new element or value to the end of a Set object in JavaScript. You have to pass the value which you want to append as an “Argument” in the Set Object “add()” method, and it then appends the specified value to the end of the Set Object.

You can append elements to a Set Object either by directly adding the values or through the variable values. We will show you both methods in the below-given example.

First of all, we will create a “setObj1” Set object:

const setObj1 = new Set();

We have created an empty Set Object; we can now add values directly using the “add()” method. For instance, to add the “It” element to our “setObj1” we will execute the following code, and it will return the setObj1 to the console after appending values:

setObj1.add("It");
console.log(setObj1);

You can also pass variables as arguments in the “add()” function to append their values in the Set object. For the demonstration, we will create two variables, “b1” and “b2,” and assign the values which we want to append in our “setObj1” Set:

const b1 = "is";
const b2 = "linuxhint.com";

We will pass both the “b1” and “b2” variables to the setObj1.add() method, and the following code will return the updated Set object:

setObj1.add(b1);
setObj1.add(b2);
console.log(setObj1);

Deleting a specific Set Object element in JavaScript

The “delete()” method is utilized to delete a specific element that exists in a Set object. For deletion, you must pass the value as an argument in the “setObj1.delete()” method.

In the below-given example, we will create a simple “setObj1” Set object and will store some numeric values such as “11”, “22”, “33”, “44”, and “55” in it:

const setObj1 = new Set([11, 22, 33, 44, 55]);

Now for once, check out the initial elements of the created “setObj1” Set, before deleting a value:

console.log("Initial setObj1 elements: ");
console.log(setObj1);

In the next step, we will delete “22” from the Set elements by using calling the “delete()” method and passing “22” as an argument:

setObj1.delete(22);
console.log("setObj1 after deleting 22 ");
console.log(setObj1);

Here is what you are likely to see after executing the provided code:

Removing All Set Object Elements in JavaScript

This section will show you how to remove all Set object elements at once by utilizing the “clear()” method. In JavaScript, the clear() method returns “undefined,” which indicates that all Set object elements are removed successfully.

We will help you understand the functionality of the “clear()” method through an example. First of all, we will create a new Set object “setObj1” and store some values in it such as “11”, “22”, “33”, “44”, and “55”:

const setObj1 = new Set([11, 22, 33, 44, 55]);

Then, we will check out the created Set Object size and its elements:

console.log("setObj1 initial size: " + setObj1.size);
console.log(setObj1);

Now, it’s time to call the “clear()” method and test its working:

setObj1.clear();

After executing the clear() method for your set object, all of the elements present in your Set Object will be removed. You can again check the size and look into your Set object:

console.log("setObj1 size after clearing elements: " + setObj1.size);
console.log(setObj1);

The following image comprises the above-given code and the output we got after execution:

Searching an element in Set Object in JavaScript

JavaScript also offers a “has()” method that can be used to check whether an element exists in a Set object or not. You have to pass the value as an argument while calling the “has()” method with your JavaScript Set object, which will return “true” if the element is found; otherwise, it will display “false”.

For instance, we will create a Set object “setObj1” having some string elements “It”, “is”, and “linuxhint.com”:

const setObj1 = new Set(["It", "is", "linuxhint.com"]);

Then, we will search for the “is” element in our “setObj1()” Set Object by calling the “has()” method and passing “is” as an argument:

console.log(setObj1.has("is"));

Now, we will search for an element “website” that is not in our Set Object to check out the output:

console.log(setObj1.has("website"));

As you can see from the output that for searching “is” the program has returned “true”, while for the “website” element, it is showing “false”, which states that “is” is a setObj1 element and “website” is not:

Conclusion

In JavaScript, add(), delete(), clear(), and has() are the most widely used object Set methods. The add() object Set method is used for appending values to the set object, delete() and clear() object set methods for deleting a specific or all elements at once, and the has() method is utilized for searching any element in the created Set. This write-up discussed the object Set methods in JavaScript. Moreover, we also demonstrated examples related to each object Set methods such as add(), delete(), clear(), and has() method, in this article.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.