Sets

Set

Is an object that stores unique elements of any kind of data type.

  • Kind of an array.

Does not allow duplicate elements.

Can be used to remove duplicates from arrays.

const charsets = new Set(['Some', 'Other', 'element']);

Array.from(Set): To show the set as array.

.size: Returns the amount of elements.

.add(el): Add an element.

  • If values already exists no error or warning is thrown.

.forEach(fn): Iterates over a Set given a function.

.has(el): Returns true if an element exists.

.delete(el): Removes a element and returns true | false.

.clear(): Removes all the elements.

Weak Set

Is a object similar to Set, but only allow values of type object.

  • Like Weak Map, also maintain weak references.

  • Also not Iterable.

.has(el): Return true if element exists.

.add(el): Add objectelement.

.delete(el): Delete element and return true | false.

Last updated