Skip to content

Set

Set intro

The Set object in JavaScript lets you store unique values of any type.

const set = new Set();

To append values to the Set object, use the add method passsing in the value as its argument.

const set = new Set();

set.add(1);
set.add(2);
set.add(1); //  -> Does not get added

To check the number of values in the set, you can use the size property.

const set = new Set();

set.add(1);
set.add(2);

set.size; // -> Returns 2

To check if a value exists in the set, use the has method.

set.has(1); // -> Returns true
set.has(5); // -> Returns false

To remove a value from the set, use the delete method passing in the value. To delete all the values in the set, use the clear method.

set.delete(1); // -> Returns true
set.delete(5); // -> Returns false

set.clear();
set.size; // -> Returns 0

Set and Array Conversion

Array to Set
const numArr = [1, 2];
const numSet = new Set([numArr]);
Set to Array
const numArr = [1, 2];
const numSet = new Set(numArr);

// just deconstruct set
[...numSet]

Iterating over a Set

const set = new Set();
set.add(1);
set.add(2);

for (let value of set) {
  console.log(value);
}
// output: 1 2