Open In App

JavaScript Set Reference

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

JavaScript Set is a collection of items that are unique i.e. no element can be repeated. Elements of the set can be iterated in the insertion order. A set can store any type of value whether primitive or objects.

Syntax:

Set.function()

Example: This example will show the use of Set() constructor.

Javascript




const mySet = new Set();
  
mySet.add("California");
mySet.add("India");
mySet.add("California");
mySet.add(10);
mySet.add(10);
  
const myObject = { a: 1, a: 5 };
  
mySet.add(myObject);
  
console.log(mySet);


Output:

Set(4) { 'California', 'India', 10, { a: 5 } }

The complete list of JavaScript Set has listed below:

JavaScript Set Constructor: A constructor gets called when an object is created using the new keyword.

Constructor Description Example
Set() Create a set of unique values of any type, whether primitive values or object preferences.
Try

JavaScript Set Properties:  A JavaScript property is a member of an object that associates a key with a value.

  • Instance Property: An instance property is a property that has a new copy for every new instance of the class.
Instance Properties Description Example
size Returns the size of set
Try

constructor Used to create a new instance of set
Try

JavaScript Set Methods: Methods are actions that can be performed on objects.

  • Instance Method: If the method is called on an instance of a Set then it is called an instance method.
Instance Methods Description Example
add() Add an element to the set with the specified value
Try

clear() Removes all the elements from the set
Try

delete() Delete specified element if it exists in the set
Try

entries() Used to iterate over each element of the set
Try

forEach() Applies a function on each element of the set
Try

has() Checks if a particular element exists in the set
Try

values() Returns an iterator that contains all elements of the set
Try

keys() Exactly equivalent to the values method
Try



Last Updated : 07 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads