JavaScript Sets:
● A JavaScript Set is a collection of unique values.
● Each value can only occur once in a Set.
How to Create a Set:
● Three Way of create javascript Set.
1. new Set()
2. add() to add values
3. add() to add variables
The new Set() method:
● The Set object lets you store unique values of any type, whether primitive values or
object references.
Example:
// Create a Set
const letters = new Set(["a", "b", "c", "c", "c", "d"]);
// Display [Link]
[Link]("demo").innerHTML = [Link];
[Link](letters); //Set(4) [ "a", "b", "c", "d" ]
Output:
4
The add() values method:
● The value of the element to add to the Set object.
Syntax:
add(value)
Example 1:
// Create a Set
const letters = new Set();
// Add Values to the Set
[Link]("a");
[Link]("b");
[Link]("c");
[Link]("c");
[Link]("c");
[Link]("d");
// Display [Link]
[Link]("demo").innerHTML = [Link];
[Link](letters); //Set(4) [ "a", "b", "c", "d" ]
Output:
4
The add() variable method:
Example 2:
// Create a Set
const letters = new Set();
// Create Variables
const a = "a";
const b = "b";
const c = "c";
const d = "c";
const e = "c";
const f = "d";
// Add the Variables to the Set
[Link](a);
[Link](b);
[Link](c);
[Link](d);
[Link](e);
[Link](f);
[Link]("demo").innerHTML = [Link];
[Link](letters); //Set(4) [ "a", "b", "c", "d" ]
Output:
4
[Link]():
● The has() method returns a boolean indicating whether an element with the specified
value exists in a Set object or not.
Example:
const set1 = new Set([1, 2, 3, 4, 5]);
[Link]([Link](1));
[Link]([Link](5));
[Link]([Link](6));
[Link]("demo").innerHTML = [Link](1);
[Link]("demo1").innerHTML = [Link](5);
[Link]("demo2").innerHTML = [Link](6);
Output:
true
true
false
The forEach() Method:
● The forEach() method call a function for each set elements.
Example:
// Create a Set
const letters = new Set(["a","b","c","c","c","d"]);
// List all Elements
let text = "";
[Link] (function(value) {
text += value + "<br>";
})
[Link]("demo").innerHTML = text;
[Link](letters); //Set(4) [ "a", "b", "c", "d" ]
Output:
a
b
c
d
The values() Method:
● The values() method returns a new iterator object containing all the values in a Set
Example:
// Create a Set
const letters = new Set(["a", "b", "c"]);
// Display [Link]
[Link]("demo").innerHTML = [Link]();
[Link](letters); //Set(3) [ "a", "b", "c" ]
Output:
[object Set Iterator]
[Link]():
● The values() method returns a new Iterator object that contains the values for each
element in the Set object in insertion order.
Example:
const mySet = new Set(["foo","bar","baz","baz","ven"]);
const setIter = [Link]();
[Link]([Link]().value);
[Link]([Link]().value);
[Link]([Link]().value);
[Link]([Link]().value);
Output:
foo
bar
baz
ven
[Link]():
● The entries() method returns a new Iterator object that contains an array of [value,
value] for each element in the Set object, in insertion order.
Example:
const mySet = new Set(["foo","bar","baz","baz","ven"]);
const setIter = [Link]();
[Link]([Link]().value);
[Link]([Link]().value);
[Link]([Link]().value);
[Link]([Link]().value);
console Output:
[ "foo", "foo" ]
[ "bar", "bar" ]
[ "baz", "baz" ]
[ "ven", "ven" ]
[Link]():
● The delete() method removes a specified value from a Set object, if it is in the set.
Example:
const mySet = new Set(["a", "b", "c", "c", "d"]);
[Link]([Link]("a"));
[Link]([Link]("b"));
[Link]([Link]("e"));
Console output:
true
true
false