JavaScript Sets


Creating  a set

Giving a new Set an Array(). Use add() to add values to a new Set. Use add() to add variables to a new Set

Method Description
new Set() Establishes a new Set.
add() A new element is added to the Set.
delete() Removes a component from a Set.
has() If a value is present in the Set, it returns true.
forEach() Callbacks are triggered for each element in the set.
values() Gives back an iterator that contains every value
Property Description
size Returns the size of a Set's element count.

1. new set() method

To add a new element to a Set object, use the set() function. The value to be added to the Set is the only parameter required. If the value is already present in the Set, nothing happens and the Set stays the same. We may chain together calls to the set() function since it returns the changed Set object.

Example

Preview

2. add() method

To add a new element to a Set object, use the add() function. The value to be added to the Set is the only parameter required. If the value is already present in the Set, nothing happens and the Set stays the same. We may repeatedly execute the add() function because it returns the updated Set object.

Example

Preview

3. delete() method

To remove an element from a Set object, use the delete() function. The value to be eliminated from the Set is the only parameter required. The delete() function will return true if the value was already removed from the Set. Nothing will happen and the delete() function will return false if the value is not present in the Set.

Example

Preview

4. has() method

To determine if a Set object includes a certain value, use the has() function. The value to be verified is the only argument needed. The has() function will return true if the value is present in the Set. The has() function will return false if the value is not present in the Set.

Example

Preview

5. foreach() method

For each Set element, a function is called using the forEach() method. A Set object's elements are iterated through using the forEach() function. It just needs one parameter, a callback function that will be used to process each Set member. The current element, the current index (which is not utilized in a Set), and the Set object being iterated over are the three parameters that the callback function requires.

Example

Preview

6. values() method

For each Set element, a function is called using the forEach() method. To acquire an iterator for the values in a Set object, use the values() function. A fresh Iterator object is returned, with the Set's contents in the chronological order they were added.

Example

Preview