0% completed
A Set in JavaScript is a collection of unique values of any type, whether primitive values or object references. Sets are particularly useful when the need to ensure the uniqueness of elements is a priority, as they automatically remove duplicates.
Syntax
To create a Set, you can use the Set()
constructor, optionally passing an iterable object (such as an array) to initialize the set with unique values from that iterable.
iterable
(optional): An array or other iterable object whose elements will be added to the new Set.
Example of Creating Sets
- Above code initializes a Set
colors
with three values: "red", "green", and "blue". Even though "red" appears twice in the array, it's only stored once in the Set.
Properties of Sets
Sets come with a property that helps to inspect the collection:
Property | Description |
---|---|
size | Returns the number of values in the Set object. |
Methods of Sets
Sets offer a variety of methods to manipulate the data:
Method | Description |
---|---|
add(value) | Adds a new element with the specified value to the Set. |
delete(value) | Removes a specified value from the Set. |
has(value) | Returns a boolean indicating whether the specified value exists in the Set. |
clear() | Removes all elements from the Set. |
values() | Returns a new Iterator object containing all the values in insertion order. |
entries() | Returns a new Iterator object containing an array of [value, value] for each element in insertion order. |
forEach(callbackFn[, thisArg]) | Executes a provided function once for each value in the Set object. |
Examples
Example 1: Adding Elements and Checking Existence
In this example, we will add elements to a Set and check if a specific value exists within the Set.
let numbers = new Set();
initializes a new empty Set namednumbers
.numbers.add(1);
adds the number1
to the Set.numbers.add(2);
adds the number2
to the Set. The Set now contains two elements:1
and2
.console.log(numbers.has(1));
checks if the Set contains the number1
. Since1
was added to the Set, it returnstrue
.
Example 2: Iterating Over a Set
In this example, we will iterate over a Set using the forEach
method and print each element.
let numbers = new Set([1, 2, 3]);
creates a Set namednumbers
with three elements:1
,2
, and3
.numbers.forEach((value) => { console.log(value); });
uses theforEach
method to iterate over each element in the Set. For each element, it executes the provided callback function, which logs the element to the console. This results in1
,2
, and3
being logged.
Example 3: Intersection of Two Sets
In this example, we demonstrate how to find the intersection of two Sets, resulting in a new Set containing only the elements present in both Sets.
let setA = new Set([1, 2, 3]);
andlet setB = new Set([2, 3, 4]);
create two Sets,setA
with elements1
,2
,3
andsetB
with elements2
,3
,4
.[...setA]
uses the spread operator to create an array containing all elements ofsetA
..filter(x => setB.has(x))
filters this array, keeping only the elements that are also present insetB
.new Set([...setA].filter(x => setB.has(x)));
creates a new Set from the filtered array, which contains only the elements present in bothsetA
andsetB
. The resulting Set isintersection
, which contains2
and3
.console.log(intersection);
logs the resulting Set to the console, showing the intersection ofsetA
andsetB
:Set {2, 3}
.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible