JavaScript

What is the JavaScript Set

In JavaScript, arrays, sets, and objects play an important role in collecting large amounts of data. A set is a unique and important data structure for collecting elements where each element can be of any type. The data in the set is arranged in an ordered format for record keeping. More specifically, there are various ways to add, delete, and clear an element from a set that is associated with the set.

This post will explain about the usage of the JavaScript set.

What is the JavaScript Set?

A variety of distinctive values make up the JavaScript Set. In a Set, a value can only appear once. A set can contain any value and any form of data. Using a JavaScript Set

How to Use a JavaScript Set?

To use the JavaScript set, follow the stated syntax:

new Set([it]);

In the above syntax, “it” is an iterable object whose all items or elements are inserted into the newly created set. If the passed arguments are null, empty, or not defined, it will create a new empty set.

Different JavaScript Set Methods

There are different functionalities associated with the set() method. Some of them are listed below:

Example 1: Add Element in JavaScript Set

To add the elements in the JavaScript set, utilize the below-stated syntax:

set.add(val);

Here, “values” are those elements that are going to be added to the set.

To do so, declare a set using “Set()” constructor:

var set = new Set();

Utilize the “set.add()” method to add the element in a set. This method will add the elements in a sequence and skip the elements that are duplicated:

set.add(37);
set.add(23);
set.add(37).add(47).add(58);

Display the output on the console with the help of the “log()” method and pass the set as the argument:

console.log(set);

It can be observed that the specified elements have been added in the set successfully:

Example 2: Delete Element in JavaScript Set

To delete the elements in the JavaScript set, follow the given syntax:

set.delete(val);

In the above syntax, “val” defines the element which will be removed or deleted from the set.

Now, first, initialize the variable and pass the value within the “Set()” constructor. Then, pass the value as the parameter of the set() method:

var set = new Set("linuxhint");

Invoke the log() method and pass the set as an argument to print it set on the console:

console.log(set);

Now, use the “set.delete()” method as the argument of the “log()” method and pass the values that need to be removed:

console.log(set.delete('n'));

console.log(set.delete('t'));

As you can see that the defined values have been deleted from the set:

Example 3: Clear JavaScript Set

To clear the JavaScript set, check out the stated syntax:

set.clear(val);

First, initialize the variable and utilize the “Set()” method and pass the values:

var set = new Set([15, 34, 43, 43, 56]);

Utilize the “set.clear()” method to clear the set:

set.clear()

Lastly, display the output on the screen by using the “log()”:

console.log(set);

It can be noticed that the set size is now “0”:

That’s all about the JavaScript set.

Conclusion

The JavaScript set consists of some unique values. There are various methods associated with the “set()” method. To insert the elements in the set, the “add()” method is utilized. Furthermore, “delete()” is utilized for removing elements, and “clear()” clears the set. This post has stated about the JavaScript set.

About the author

Hafsa Javed