JavaScript Set Object

The JavaScript Set object is used to store the elements with unique values. The values can be of any type i.e. whether primitive values or object references.

Syntax

new Set([iterable])  

Parameter

iterable – It represents an iterable object whose elements will be added to the new Set.

Points to remember

  • A set object uses the concept of keys internally.
  • A set object cannot contain the duplicate values.
  • A set object iterates its elements in insertion order.

JavaScript Set Methods

Let’s see the list of JavaScript set methods with their description.

MethodsDescription
add()It adds the specified values to the Set object.
clear()It removes all the elements from the Set object.
delete()It deletes the specified element from the Set object.
entries()It returns an object of Set iterator that contains an array of [value, value] for each element.
forEach()It executes the specified function once for each value.
has()It indicates whether the Set object contains the specified value element.
values()It returns an object of Set iterator that contains the values for each elemants.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *