In JavaScript, a Set is a built-in object that allows you to store unique values of any type, whether primitive values or object references. The values can be added or removed from the Set, and you can also check if a value exists in the Set using its methods. Unlike arrays, Sets are not ordered and do not have indexes.
To create a set in JavaScript, you can use the built-in Set object. Here's an example:
const mySet = new Set([1, 2, 3]);
In this example, mySet
is a Set object that contains the values 1, 2, and 3.
You can also create an empty set and add values to it using the add()
method:
const mySet = new Set();
mySet.add(4);
mySet.add(5);
mySet.add(6);
In this example, mySet
starts as an empty Set, and the add()
method is used to add the values 4, 5, and 6 to it.
In JavaScript, the order of elements in a Set is insertion order. This means that elements are stored in the order they were added to the Set and will be retrieved in that same order.
In JavaScript, set size refers to the number of unique elements (values) in a Set object. The size property of a Set object can be used to retrieve this value. For example:
const mySet = new Set([1, 2, 3, 3, 4]);
console.log(mySet.size); // Output: 4
In the above code snippet, mySet
contains 5 elements, but since there are only 4 unique values (1, 2, 3, and 4), the size of the set is 4.
The Set.has()
function in JavaScript is a method that is used to check if a particular value exists in a Set object. It returns a boolean value indicating whether the specified element is present in the Set or not.
The Set.delete()
function in JavaScript is a method used to remove an element from a Set object. This method returns a boolean value indicating if the element was successfully deleted or not. The syntax for using this function is as follows:
set.delete(value)
where set
is the Set object and value
is the element to be removed. If the element is present in the Set object, it will be removed and true
will be returned. If the element is not present in the Set object, no action is taken and false
is returned.
In JavaScript, you can use the indexOf()
method to get the index of an element in an array. For example:
const myArray = ['apple', 'banana', 'orange'];
const index = myArray.indexOf('banana');
console.log(index); // Output: 1
If the element is not present in the array, the method will return -1.
To print set values using a for-of loop in JavaScript, you can use the following code:
const mySet = new Set([1, 2, 3]);
for (let value of mySet) {
console.log(value);
}
This will iterate through all the values in the set and print them to the console. You can replace "mySet" with the name of your own set.
To convert a Set to an array in JavaScript, you can use the spread operator (...
) and wrap the Set instance with square brackets. Here's an example:
const mySet = new Set(['apple', 'banana', 'orange']);
const myArray = [...mySet];
console.log(myArray); // Output: ['apple', 'banana', 'orange']
Alternatively, you can also use the Array.from()
method to create an array from a Set:
const mySet = new Set(['apple', 'banana', 'orange']);
const myArray = Array.from(mySet);
console.log(myArray); // Output: ['apple', 'banana', 'orange']
The foreach
function is not applicable to sets in all programming languages, but generally it allows for iterating over each element in a set and performing a specified action for each element. This can be useful when you need to process every item in a set without knowing its specific size or structure.