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 set.size
document.getElementById("demo").innerHTML = letters.size;
console.log(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
letters.add("a");
letters.add("b");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("d");
// Display set.size
document.getElementById("demo").innerHTML = letters.size;
console.log(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
letters.add(a);
letters.add(b);
letters.add(c);
letters.add(d);
letters.add(e);
letters.add(f);
document.getElementById("demo").innerHTML = letters.size;
console.log(letters); //Set(4) [ "a", "b", "c", "d" ]
Output:
4
Set.prototype.has():
● 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]);
console.log(set1.has(1));
console.log(set1.has(5));
console.log(set1.has(6));
document.getElementById("demo").innerHTML = set1.has(1);
document.getElementById("demo1").innerHTML = set1.has(5);
document.getElementById("demo2").innerHTML = set1.has(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 = "";
letters.forEach (function(value) {
text += value + "<br>";
})
document.getElementById("demo").innerHTML = text;
console.log(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 set.size
document.getElementById("demo").innerHTML = letters.values();
console.log(letters); //Set(3) [ "a", "b", "c" ]
Output:
[object Set Iterator]
Set.prototype.values():
● 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 = mySet.values();
console.log(setIter.next().value);
console.log(setIter.next().value);
console.log(setIter.next().value);
console.log(setIter.next().value);
Output:
foo
bar
baz
ven
Set.prototype.entries():
● 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 = mySet.entries();
console.log(setIter.next().value);
console.log(setIter.next().value);
console.log(setIter.next().value);
console.log(setIter.next().value);
console Output:
[ "foo", "foo" ]
[ "bar", "bar" ]
[ "baz", "baz" ]
[ "ven", "ven" ]
Set.prototype.delete():
● 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"]);
console.log(mySet.delete("a"));
console.log(mySet.delete("b"));
console.log(mySet.delete("e"));
Console output:
true
true
false