JavaScript Set Reference
Last Updated :
07 Jun, 2023
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. |
|
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 |
|
| constructor |
Used to create a new instance of set |
|
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 |
|
| clear() |
Removes all the elements from the set |
|
| delete() |
Delete specified element if it exists in the set |
|
| entries() |
Used to iterate over each element of the set |
|
| forEach() |
Applies a function on each element of the set |
|
| has() |
Checks if a particular element exists in the set |
|
| values() |
Returns an iterator that contains all elements of the set |
|
| keys() |
Exactly equivalent to the values method |
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...