Skip to content

Set

A Set object lets you store unique values of any type, whether primitive values or object references. It remembers insertion order for iteration.

add(value) function

Add a new element with the given value to the Set, if it’s not already present.

value: The value to add.

Returns: The Set object (for chaining).

has(value) function

Return a boolean indicating whether the Set contains the specified value.

value: The value to check for presence in the Set.

Returns: True if the value is found, otherwise false.

delete(value) function

Remove the specified value from the Set if it exists.

value: The value to remove.

Returns: True if the value was present and removed, otherwise false.

clear() function

Remove all elements from the Set, leaving it empty.

Returns: None

size accessor

(read only)

A read-only property returning the number of elements in the Set.

Returns: The number of unique values in the Set.

forEach(callbackFn, thisArg) function

Execute a provided callback function once for each value in the Set, in insertion order.

callbackFn: A function(value, valueAgain, set) to execute on each element.

thisArg: Optional. A value to use as 'this' when executing callbackFn.

Returns: None

values() function

Alias for values() in a Set. Return a new Iterator object containing all the values (as keys) in the Set, in insertion order.

Returns: An iterator of the Set's values.

keys() function

Alias for values() in a Set. Return a new Iterator object containing all the values (as keys) in the Set, in insertion order.

Returns: An iterator of the Set's values.

entries() function

Return a new Iterator object containing [value, value] pairs for each value in the Set, in insertion order. This maintains API consistency with Map objects.

Returns: An iterator of [value, value] pairs.