Set() constructor in javascript
The set constructor creates new set objects to store unique values. Values can be of any type of primitive values or object references. The set constructor contains single values if multiple same values are available.
Set.method()
To make set editable, there are some methods.
Set.add()
It adds a new value .
const setn=new Set();
setn.add(4);
Set.clear()
It clears all values from the object.
const setn=new Set();
setn.clear();
Set.delete()
It delets specified value from the object.
const setn=new Set();
setn.add(4);
setn.delete(4); //deletion
Set.entries()
It returns [Set Entries] {[value,value]}.
const setn=new Set();
setn.add(8);
setn.entries(); //returns [Set Entries] {[8,8]}
Set.forEach()
It is used to looping for every value of set object.
const setnew=new Set();
setnew.add(4);
setnew.add(5);
//print every value as This is value:value
setnew.forEach(e=>console.log("This is value:",e));
Syntax
const setname = new set();
const setname=new set(iterable);
Parameters
iterable
When an iterable is passed to the set, the new set will take all elements of the iterable. If iterable is not passed, the new set will be null.
Return Value
It returns a new set
object.
Example
Leu's create a new Set of numbers.