JS 7

  • Will add smaller content to compensate

A Set() is a list-like data structure that can't be accessed through index like arrays:

//we add new elements to a set with .add()

let set= new Set()
set.add(1)
set.add( {tin: 10, ten:"dieci"})

It can't have more than one strict equal element (===):

//we can use .size property to count the elements in it

let uno= new Set()
uno.add(1)
uno.add(1)
uno.add({uno: 1})
uno.add({uno: 1)}

uno.size            //Set(3) {1, {…}, {…}}
//the 2 objects arent strict equals so it will add

We use add(), delete() and has() methods on Sets:

We can use both the forEach() and for() loop on Sets:

To pass from Set to Array and vice-versa:

We can use Sets to get intersections and difference sets:

Last updated