Initialize a Set in JavaScript
This post will discuss how to initialize a Set in JavaScript.
Initializing a set means creating a new set object and optionally adding some values to it. There are several methods to initialize a set in JavaScript, depending on what kind of values we want to add to the set and how we want to pass them. Here are some of the common functions:
1. Using Set() constructor
We can pass an array of values to the Set() constructor to create a new set that contains the unique values from the array. This is a simple and convenient way to initialize a set with multiple values at once. For instance:
|
1 2 3 4 5 6 7 8 |
// Create an array of values const arr = [1, 2, 3, 4, 5]; // Create a new set from the array const mySet = new Set(arr); // Log the set console.log(mySet); // Set(5) { 1, 2, 3, 4, 5 } |
We can even pass a string of characters to the Set() constructor to create a new set that contains the unique characters from the string. This is useful when we want to initialize a set with individual characters or symbols. For instance:
|
1 2 3 4 5 6 7 8 |
// Create a string of characters const str = "hello"; // Create a new set from the string const mySet = new Set(str); // Log the set console.log(mySet); // Set(4) { 'h', 'e', 'l', 'o' } |
Another option is to create a new set object from another iterable object, such as another set, using the Set() constructor. For example, this code creates a new set from an existing set.
|
1 2 3 4 5 6 7 8 |
// Create a set let mySet = new Set([1, 2, 3]); // copy is a new set with the same elements as mySet let copy = new Set(mySet); // Log the copy set console.log(copy); // Set(3) { 1, 2, 3 } |
2. Using add() function
We can use the add() function of the Set object to add one or more elements to an existing set object. The add() function takes a value as an argument and adds it to the set if it does not already exist. We can create an empty set by calling the Set() constructor without any arguments, and then use the add() function to add values to the set one by one. This is useful when we want to initialize a set dynamically or conditionally. For instance:
|
1 2 3 4 5 6 7 8 9 10 |
// Create an empty set const mySet = new Set(); // Add values to the set mySet.add(1); mySet.add(2); mySet.add(3); // Log the set console.log(mySet); // Set(3) { 1, 2, 3 } |
The add() function returns the set object itself, allowing us to chain multiple calls to it. For example, this code creates an empty set and then adds three elements using chaining:
|
1 2 3 4 5 6 7 8 |
// Create an empty set const mySet = new Set(); // Add values to the set mySet.add(1).add(2).add(3); // Log the set console.log(mySet); // Set(3) { 1, 2, 3 } |
That’s all about initializing a Set in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)